2

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>"; 
$result = 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>";
2
  • don't use die, the xml is dead! Commented Aug 24, 2011 at 11:57
  • Avoid using the mysql functions, instead use mysqli, it's better in all ways and mysql is on the road to being deprecated Commented Aug 24, 2011 at 14:13

4 Answers 4

6

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>";
Sign up to request clarification or add additional context in comments.

5 Comments

The mysql_num_rows followed by the while loop is an anti-pattern. Replace it with $row = mysql_fetch_array($result, MYSQL_ASSOC); if( !$row ){/*handle no results*/} else{do{ /* handle rows */ }while($row = mysql_fetch_array($result, MYSQL_ASSOC));}
i was unaware of this thing.. i checked in my end and found working.. please suggest me a documentation so that i can understand more.. also it would be helpful if you could edit my answer.
@cwallenpoole: In my book, do..while loops are anti-patterns, because they often imply code duplication such as the call to mysql_fetch_array() in your example.
@cwallenpoole - I don't have a problem with how mithunsatheesh wrote it. The idiom of peeling off a row to test for emptiness before entering the loop, on the other hand, makes my skin crawl.
@mithunsatheesh: Your forgot to remove the or die or move it in the if (!$result) statement below it. It's another code duplication, this time made by the author of the question.
0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.