Log in

View Full Version : Exclude mysql column from array



lindm
07-16-2008, 06:50 PM
I have a function to reset all columns of a mysql database in php. I however want to exclude some columns from this reset. Need som help...


$qColumnNames = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error");
$numColumns = mysql_num_rows($qColumnNames);
$x = 0;
while ($x < $numColumns)
{
$colname = mysql_fetch_row($qColumnNames);
$col[$colname[0]] = $colname[0];
$x++;
}
$querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\'';

mysql_query($querycx);

dicegame
07-17-2008, 07:30 AM
You should be able to call each colname by using an index e.g. [0] or [3], you can use an if and else to disable sifting through certain columns. e.g.

This should be in between the while brackets.


if ($colname[1] || $colname[5]) {

} else {

$querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\'';

mysql_query($querycx);

}

lindm
07-17-2008, 07:50 PM
Solved it now:


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



?>

blm126
07-18-2008, 05:28 PM
This is really the long way around the problem. Use SQL to get the column names you want. Here is the query you would need. I'm not really sure what you want to do with this data though.


SELECT column_name FROM information_schema.columns WHERE table_name = '$table' AND column_name NOT IN('userId', 'userName', 'userPass', 'firma', 'orgnr', 'Scroll')