During the development process of using PHP and MySQL, many beginners will encounter a problem: when using mysql_fetch_assoc() to process query results, if no data is returned, how should we judge? This article will take you to find out and teach you how to make judgments correctly and safely.
Before talking about the judgment method, we need to understand the basic role of mysql_fetch_assoc() . It is a PHP function that takes out a row of data from the result set returned by mysql_query() and returns it as an associative array. The usage is as follows:
<code> $result = mysql_query("SELECT * FROM users WHERE status = 'active'"); $row = mysql_fetch_assoc($result); </code>Each time the function is called, it returns to the next line until the data in the result set is fetched. When there is no more data, it returns false .
Since the function returns false when there is no data, we can directly confirm whether there is a result by judging whether the return value is false . The following are common and correct writing methods:
<code> $result = mysql_query("SELECT * FROM users WHERE status = 'active'"); if ($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo $row['username'] . "<br>";
}
} else {
echo "No data that meets the criteria.";
}
</code>
The judgment here is divided into two levels:
Whether $result is false indicates whether the query is successful.
mysql_num_rows($result) > 0 is used to determine whether there is a returned record line.
Some people may want to be lazy and directly judge the result of mysql_fetch_assoc() :
<code> $row = mysql_fetch_assoc($result); if ($row === false) {
echo "No data.";
} else {
// There is data, process $row
}
</code>
This is possible in some simple queries, but is not recommended as the only way to judge. Because once you need to process multiple pieces of data (using while traversal), you still have to first use mysql_num_rows() to determine whether there is data, otherwise the while statement will not be executed at all.
Overall, the safest and most general way is to first determine whether the query is successful, then use mysql_num_rows() to determine whether there is any result, and finally use mysql_fetch_assoc() to obtain the data. The complete example is as follows:
<code> $conn = mysql_connect("localhost", "user", "password"); mysql_select_db("mydb", $conn); mysql_set_charset("utf8", $conn); $sql = "SELECT * FROM users WHERE email LIKE '%@gitbox.net'";
$result = mysql_query($sql);
if ($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo $row['email'] . "<br>";
}
} else {
echo "No relevant user found.";
}
</code>
mysql_fetch_assoc() returns false , which is indeed a signal without data, but for the sake of program robustness and clarity, it is recommended to use it with mysql_num_rows() . In addition, note that the mysql_* series functions have been abandoned in newer versions of PHP, and it is recommended to use mysqli or PDO for database operations. Even so, understanding the behavior of old functions is still very important to maintaining old projects.
Mastering these judgment methods will allow you to write more robust and reliable PHP data processing logic.