mysql_num_rows不起作用(不会返回0)

时间:2014-04-27 02:23:11

标签: mysql sql zero string

我创建了一个条件,如果查询返回结果,它将执行代码并返回1或更多。但是,如果查询为空,我们将得到0并且if语句不会激活。

但是我已经获得1并且if语句激活,尽管没有返回任何行(在phpmyadmin SQL执行器中测试它。

一个提示是它说'你的SQL查询已成功执行',而没有行返回,如果说'MySQL返回一个空的结果集'。

发生了什么,我该如何解决这个问题?

$search_num_rows_2 = mysql_query("SELECT * FROM (SELECT tags_movie.name_movie, tags_game.name_game, tags_sport.name_sport, tags_music.name_music, tags_show.name_show FROM tags_game, tags_movie, tags_sport, tags_music, tags_show WHERE tags_game.relation = '$user_id' && tags_movie.relation = '$user_id' && tags_sport.relation = '$user_id' && tags_music.relation = '$user_id' && tags_show.relation = '$user_id') somealias WHERE name_movie LIKE '{$movie}%' && name_game LIKE '{$name_game}%' &&name_sport LIKE '{$sport}%' &&name_music LIKE '{$music}%' && name_show LIKE '{$show}%'");

$number = mysql_num_rows($search_num_rows_2);
echo $number;
if ($number != 0){

    }

1 个答案:

答案 0 :(得分:0)

根据我的理解,你没有实现在if块中捕获空结果,它应该是,

if ($number != 0){
   echo $number;
} else {
   echo 'Empty result';
}

编辑1

$res = mysql_query("SELECT * FROM (SELECT tags_movie.name_movie, tags_game.name_game, tags_sport.name_sport, tags_music.name_music, tags_show.name_show FROM tags_game, tags_movie, tags_sport, tags_music, tags_show WHERE tags_game.relation = '$user_id' && tags_movie.relation = '$user_id' && tags_sport.relation = '$user_id' && tags_music.relation = '$user_id' && tags_show.relation = '$user_id') somealias WHERE name_movie LIKE '{$movie}%' && name_game LIKE '{$name_game}%' &&name_sport LIKE '{$sport}%' &&name_music LIKE '{$music}%' && name_show LIKE '{$show}%'");
$data = mysql_fetch_array($res);
if (count($data) > 0){
       print_r($data);
} else {
       echo 'Empty result';
}