note 116711 added to mysql.examples-basic
| From: | zjrwjzzz at 163 dot com | Date: | Fri, 13 Feb 2015 17:03:28 +0000 |
| Subject: | note 116711 added to mysql.examples-basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201731@lists.php.net to get a copy of this message | ||
Be aware that if you are trying to foreach the associative array returned by mysql_fetch_array()
with MYSQL_ASSOC or mysql_fetch_assoc(), you have to ensure the return value that is used in foreach
structure is exactly a array rather than a FALSE value.
Or you might encounter the warning below:
Warning: Invalid argument supplied for foreach()
Here is an example (assume that $result have already stored the return value from mysql_query()):
If you have some block of code like:
<?php
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
?>
If this is the case, you should use the code below instead:
<?php
// Check ahead, before using it
if (mysql_num_rows($result) > 0) {
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 123.112.106.57
----
Manual Page -- http://php.net/manual/en/mysql.examples-basic.php
Edit -- https://master.php.net/note/edit/116711
Del: integrated -- https://master.php.net/note/delete/116711/integrated
Del: useless -- https://master.php.net/note/delete/116711/useless
Del: bad code -- https://master.php.net/note/delete/116711/bad+code
Del: spam -- https://master.php.net/note/delete/116711/spam
Del: non-english -- https://master.php.net/note/delete/116711/non-english
Del: in docs -- https://master.php.net/note/delete/116711/in+docs
Del: other reasons-- https://master.php.net/note/delete/116711
Reject -- https://master.php.net/note/reject/116711
Search -- https://master.php.net/manage/user-notes.php