开发者

PHP Mysql If statement not working

Got a issue where if the rows are equal to 0 the else statement is not called. if i type in the correct details on the site, the first condition is met and the xml is displayed, however if the incorrect details are entered, the error xml is not displayed.

echo "<users>"; 
$resul开发者_Python百科t = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());

if (!$result) die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if(mysql_num_rows($result) != 0)
    {
        echo "<usercallback>";
        echo  "<id>".$row['id']."</id>";
        //echo  "<number>".$row2['number']."</number>";
        //echo  "<gender>".$row2['gender']."</gender>";
        //echo  "<relationship>".$row2['relationship']."</relationship>";
        echo  "</usercallback>";
    }
    else
    {
        echo "<usercallback>";
        echo  "<id>error</id>";
        echo  "</usercallback>";
    }
}

echo "</users>";


actually you have to put the if mysql_num_rows check outside the while block

echo "<users>"; 
$result = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());


if(mysql_num_rows($result) != 0)
    {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {       
        echo "<usercallback>";
        echo  "<id>".$row['id']."</id>";
        //echo  "<number>".$row2['number']."</number>";
        //echo  "<gender>".$row2['gender']."</gender>";
        //echo  "<relationship>".$row2['relationship']."</relationship>";
        echo  "</usercallback>";
        }
    }
    else
    {
        echo "<usercallback>";
        echo  "<id>error</id>";
        echo  "</usercallback>";
    }

echo "</users>";


while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

If there are no rows then $row will be false and it will never enter the above while loop. So you can not get error message


You check for rows while performing the loop, you should check for the number of rows BEFORE the while loop.


It looks like you're expecting a zero-row response from MySQL to result in an undef $result. That's not what happens. mysql_query() returns null if there was an error. You can test the "truth" of its return value to see if the query ran correctly. If the query results in 0 rows of response, it ran correctly. The value to test, then is mysql_num_rows($result).

However, it can be done simpler. I usually write this like:

$res = mysql_query("SELECT 1");
if (!$res) {
    //do whatever error reporting if the SQL was bad
    //though you should probably deal with any condition that ends up
    //here before you go into production!
}
if (mysql_num_rows($res) == 0) {
    //put out my "no results found" message
}
while ($row = mysql_fetch_assoc($res)) {
    //do my per-row business
}

mysql_fetch_assoc() returns false when there are no (or no more) results to return. So if it's empty, that while loop will never run, so it doesn't need to be inside an else.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜