开发者

php while() loop within if / else not seeming to work

开发者 https://www.devze.com 2023-02-19 20:21 出处:网络
I\'ve been confused about why this wont work! My query executes perfectly fine in all aspects, but when I added the if check on the $row value, the echo message goes as expected (if there are no resul

I've been confused about why this wont work! My query executes perfectly fine in all aspects, but when I added the if check on the $row value, the echo message goes as expected (if there are no results), but the else will not kick when there is a match in my query??? Why is this not working? Someone please ease my pain and set me straight!!

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

$i = 1;
while($row = mysq开发者_开发百科l_fetch_array($result)) {
    echo "$i - " . $row['first_name'] . " " .  $row['last_name'] . " - " .   $row['address'] . "<br />";
$i++;
    } 
}


$row = mysql_fetch_array($result);  // this fetches the first row your result
...

in while loop you are again fecthing thr row from result. Which will not work if you have only one row in resul

use

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++;
    } 
}


try mysql_num_rows($result)==0 instead in the if statement

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号