if/else中的php while()循环似乎不起作用

if/else中的php while()循环似乎不起作用,php,mysql,if-statement,while-loop,Php,Mysql,If Statement,While Loop,我一直不明白为什么这行不通!我的查询在所有方面都执行得很好,但是当我在$row值上添加if检查时,echo消息会按预期进行(如果没有结果),但是当我的查询中有匹配项时,else不会启动???为什么这不起作用?有人请减轻我的痛苦,让我平反 $row = mysql_fetch_array($result); if (!$row) { echo "Sorry brah. Nothing matches your search criteria."; } else { $i = 1; whi

我一直不明白为什么这行不通!我的查询在所有方面都执行得很好,但是当我在$row值上添加if检查时,echo消息会按预期进行(如果没有结果),但是当我的查询中有匹配项时,else不会启动???为什么这不起作用?有人请减轻我的痛苦,让我平反

$row = mysql_fetch_array($result);
if (!$row) {
    echo "Sorry brah. Nothing matches your search criteria.";
} else {

$i = 1;
while($row = mysql_fetch_array($result)) {
    echo "$i - " . $row['first_name'] . " " .  $row['last_name'] . " - " .   $row['address'] . "<br />";
$i++;
    } 
}
$row=mysql\u fetch\u数组($result);
如果(!$row){
echo“对不起,布拉。没有任何内容符合您的搜索条件。”;
}否则{
$i=1;
while($row=mysql\u fetch\u数组($result)){
回显“$i-”$row['first_name']。“$row['last_name']。”-“$row['address']。”
”; $i++; } }
在while循环中,您再次从结果中删除行。如果结果中只有一行,则该选项将不起作用

使用

if(mysql\u num\u行($result)==0){
echo“对不起,布拉。没有任何内容符合您的搜索条件。”;
}否则{
$i=1;
while($row=mysql\u fetch\u数组($result)){
回显“$i-”$row['first_name']。“$row['last_name']。”-“$row['address']。”
”; $i++; } }
试试
mysql\u num\u rows($result)==0
而不是在if语句中

我自由地编辑了您的答案
mysql\u num\u rows($result)>0
,应该是
mysql\u num\u rows($result)==0
。希望你不介意。谢谢你的澄清。解决了的!
$row = mysql_fetch_array($result);  // this fetches the first row your result
...
if (mysql_num_rows($result) == 0) {
    echo "Sorry brah. Nothing matches your search criteria.";
} else {
    $i = 1;
    while($row = mysql_fetch_array($result)) {
        echo "$i - " . $row['first_name'] . " " .  $row['last_name'] . " - " .   $row['address'] . "<br />";
        $i++;
    } 
}