Search code examples
phpmysql-num-rows

mysql_num_rows in an if statement


The problem I am facing is, mysql_num_rows gives me an output of 1 all through out the code, but when I match it wil 0 in an if statement, it returns true and does the code. so $license returns ........ instead of its actual value.

I tried to debug the problem myself using these.

  • Tried print_r to see if datas exists. - Yes.
  • Tried echoing the $license at first part - returns the right value.
  • Tried checking the value of mysql_num_rows - returns 1.
  • Matching it with 0 in an if statement - returns true when it should be false since the value is 1.

Any help on this?

$check = mysql_query("SELECT * FROM licenses WHERE email='$email'") or die(mysql_error
                                                                           ());
if (mysql_num_rows($check) > 0)
{
    while ($data = mysql_fetch_array($check))
    {
        print_r($data); // for test
        $name = $data['name'];
        $license = $data['pid'];
        echo $license; // test print 1
        $comments = $data['comments'];
    }

    if ($license == "Sgsmorgan")
        $license = "EWP Discounted Basic (Simpleleveraging)";
}

$count = mysql_num_rows($check); // for test
echo $count; // returns 1.
if (mysql_num_rows($check) == 0)
    $name = "";
$license = "...........";
echo $license;// test print 2
$comments = "Email doesnt exist in the database";

Solution

  • Surely you mean this:

    if (mysql_num_rows($check)==0)
    {
        $name = "";
        $license = "...........";
        echo $license; //Test print 2
        $comments = "Email doesnt exist in the database";
    }
    

    Rather than

    if (mysql_num_rows($check)==0)
    $name = "";
    $license = "...........";
    echo $license; //Test print 2
    $comments = "Email doesnt exist in the database";
    

    Not using the curly brackets means only the first line below the if statement is included within it. So $license is always set to ............

    Always use curly brackets.