Jump to content

Recommended Posts

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;




?>

 

 

<?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.

 

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);

?>

 

 

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;



?>

 

 

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!

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.