我需要一种方法来做到这一点,如果一个mysql查询不能检索任何数据,就会发生一些事情.这是一个例子.
$color="red";
$query = mysql_query("SELECT * FROM people WHERE shirt='$color'");
if($query = null){
echo 'No people wearing '.$color' shirts available.';
}
Run Code Online (Sandbox Code Playgroud)
使用mysql_num_rows()此.
if( mysql_num_rows($query) == 0 ) {
// No results returned
Run Code Online (Sandbox Code Playgroud)
使用mysql_num_rows检查,多少行已被查询返回.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
echo "$num_rows Rows\n";
}
?>
Run Code Online (Sandbox Code Playgroud)