Invalid argument supplied for foreach()

seems like I have an array problem…

this is a script to rename all the fields in all tables to remove white spaces. Such as, changing the field name ‘invoice by’ to ‘invoiceby’

this is a script that I found. First I had the problem with the query function, but now I am on to the bolded line below.
foreach ($columnstemp as $columntemp)

hope someone can help me!

//just to help prevent the script from timing out
ini_set(‘memory_limit’, ‘100M’);

//give us something to look at on the screen
echo “

Start Process…


//create a database connection
$mysql_hostname = ‘localhost’;
$mysql_user = ‘username’;
$mysql_password = ‘password’;
$mysql_database = ‘databaseName’;
$conn = mysql_connect($mysql_hostname,$mysql_user,$mysql_password);
mysql_select_db($mysql_database, $conn);

//build up the list of tables to process
$sql = “SHOW TABLES”;
$tables = array();
echo ‘

Building audit tables list…

//$tablestemp = $db->query($sql,2); this line didn’t work for me so I changed it to the line below and found a query function online
$tablestemp = query($sql,2);
foreach ($tablestemp as $tabletemp)
$tables[] = $tabletemp;
echo ‘

Audit table list built.


//run through each table, build up a column list and then run an update SQL statement against the column
foreach($tables as $table)
$sql = "SHOW COLUMNS FROM $table";
$columns = array();
echo '

Building audit columns list for ’ . $table . ‘…

//$columnstemp = $db->query($sql,2); this line didn’t work for me so I changed it to the line below and found a query function online
$columnstemp = query($sql,2);
foreach ($columnstemp as $columntemp)
$columns[] = $columntemp;
echo ‘

Audit column list for ’ . $table . ’ built.

foreach($columns as $column)
	//trim and replace in one foul SQL swoop!
$sql = "UPDATE `$table` SET `" . $column['Field'] . "` = TRIM(REPLACE(REPLACE(REPLACE(`" . $column['Field'] ."`,\"'\",\"'\"),'\"','"'),'~','''))";
echo "<p>$sql</p>";

echo “

End Process.


and here is the query function that I used:
[php]function query($query) {
if(!$query) return “”;

$result = mysql_query($query) or die("MySQL Error : " . mysql_error() . "\n&lt;br />In Query &lt;code>$query&lt;/code>");

if(mysql_num_rows($result) == 1) { //If there is just one result, return it.
    $arr = mysql_fetch_assoc($result);
    if(count($arr) == 1) { //If there is just one result...
        $item = array_values($arr);
        return stripslashes($item[0]); // Creates the effect of 'getOne()'
    } else {
        foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
        return $arr; // Makes the 'query()' effect
} else {
    $arr = array();
    $primary_key = false;

    while ($all = mysql_fetch_row($result)) {
        if(count($all) == 1) array_push($arr,$all[0]);
        elseif(count($all) == 2) {
            if(!$primary_key) { //Happens only one time
                $meta = mysql_fetch_field($result,0); //If the first element is a primary key, make
                $primary_key = $meta->primary_key;        // the result an associative array.
            //Make it an Associative Array if there is a primary_key in the given data.
            else break;
            if($primary_key) $arr[$all[0]] = $all[1];
        else break;
    if($arr) {
        //Do a stripslashes() on each element
        foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
        return $arr; // For 'getAll()'

    } else { //If nothing matches...
        mysql_data_seek($result,0); //Reset the Query.
        return $result; // This results in 'getSqlHandle()' effect


In your function call you have 2 arguments:


But in function definition, there is only one.
Also, when you say code didn’t work, do you mean you are getting error message?

…I forgot to remove the second argument in query($sql,2); when I posted the php…sorry

The error message I am getting is

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\cleanfieldnames.php on line 90

from what I understand that means it is not an array?

Line 90 is:
foreach ($columnstemp as $columntemp)

Yes, it seem your function returned scalar value. For debugging purpose add this code:
[php]var_dump($columnstemp); die();[/php]

(this will show you if this is an array or not)

it returned

of type (mysql result)

Then try to:
[php]echo mysql_error();[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service