lindm Posted July 17, 2008 Share Posted July 17, 2008 I have this query to set all columns of a table to nothing. Now I want to exclude some of the columns from the query. How to? <?php include ('mysqlconnnection.php'); //include connection $result = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($result); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($result); $col[$colname[0]] = $colname[0]; $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\''; echo $querycx; ?> Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/ Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 Your going to have to explicitly name each colum I believe. UPDATE tableName SET col1='' col2='' col5='' WHERE username = 'username' And just dont specify the colums you don't want to change. Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592431 Share on other sites More sharing options...
samshel Posted July 17, 2008 Share Posted July 17, 2008 <?php include ('mysqlconnnection.php'); //include connection $arrExclude = array('col1', 'col2'); $result = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($result); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($result); if(!in_array($colname, $arrExclude) { $col[$colname[0]] = $colname[0]; } $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\''; echo $querycx; ?> $arrExclude can contain all columns that need not be updated. Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592433 Share on other sites More sharing options...
lindm Posted July 17, 2008 Author Share Posted July 17, 2008 Get an error Parse error: syntax error, unexpected '{' in /Library/WebServer/Documents/clear.php on line 16 Current script <?php include ('mysqlconnnection.php'); //include connection $arrExclude = array('id', 'userName', 'userPass', 'firma', 'orgnr', 'Scroll'); $result = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($result); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($result); if(!in_array($colname, $arrExclude) { $col[$colname[0]] = $colname[0]; } $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\''; mysql_query($querycx); ?> Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592442 Share on other sites More sharing options...
samshel Posted July 17, 2008 Share Posted July 17, 2008 Try this line: added ")" at the end of condition if(!in_array($colname, $arrExclude)) { sorry the code was not tested properly. Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592443 Share on other sites More sharing options...
lindm Posted July 17, 2008 Author Share Posted July 17, 2008 Hi samshel, The code executes now but the excluded columns are still included in the query, strange. If you see any obvious solution please let me know. Here is the current code (echo query instead of mysql_query until the code is ready). <?php include ('mysqlconnnection.php'); //include connection $arrExclude = array('id', 'userName', 'userPass', 'firma', 'orgnr', 'Scroll'); $result = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($result); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($result); if(!in_array($colname, $arrExclude)) { $col[$colname[0]] = $colname[0]; } $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\''; echo $querycx; ?> Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592453 Share on other sites More sharing options...
lindm Posted July 17, 2008 Author Share Posted July 17, 2008 Solved it now. Used array_diff to create a new array: <?php include ('mysqlconnnection.php'); //include connection $arrExclude = array('userId', 'userName', 'userPass', 'firma', 'orgnr', 'Scroll'); $result = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($result); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($result); if(!in_array($colname, $arrExclude)) { $col[$colname[0]] = $colname[0]; } $x++; } $col2= array_diff($col,$arrExclude); //removes arrExclude from the created array $col $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col2 ) .'=\'\''; //new array is used in mysql query mysql_query($querycx); ?> Thanks for all help! Quote Link to comment https://forums.phpfreaks.com/topic/115228-solved-exclude-column-from-php-generated-query/#findComment-592746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.