This works:
$customerBox = mysql_query("MY SQL STATEMENT HERE");
$boxRow = mysql_fetch_array($customerBox);
$customerBox = mysql_query("MY SQL STATEMENT AGAIN");
while($item = mysql_fetch_assoc($customerBox)) {
foreach ($item as $columnName => $value) {
if (empty($value)) {
print $columnName;
}
}
}
This does not:
$customerBox = mysql_query("MY SQL STATEMENT HERE");
$boxRow = mysql_fetch_array($customerBox);
while($item = mysql_fetch_assoc($customerBox)) {
foreach ($item as $columnName => $value) {
if (empty($value)) {
print $columnName;
}
}
}
Why? I guess I don’t understand how variables work yet.
The problem is because since the query returns one row, there’s nothing left to fetch.
the
mysql_fetch_*functions fetch the current row and then advance the row pointer to the next one. If the current row doesn’t exist, it returns false. So on your second call tomysql_fetch_assoc, the pointer is on the 2nd row, but that row doesn’t exist so your loop isn’t executed. You have two options:Good: Remove the
whileloop, and changeforeachto use$boxRowinstead:Ok: Rewind MySQL’s row pointer using
mysql_data_seek: