1

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";
11
  • 2
    The mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Speaking of, the sample code is potentially vulnerable to SQL injection. Parameterize the statement to close the vulverability. Commented Mar 25, 2012 at 7:19
  • 3
    Don't use SELECT * unless you're writing a DB administration program; select only the columns you need. Commented Mar 25, 2012 at 7:20
  • 2
    For readability's sake, please pick and apply an indent style. Commented Mar 25, 2012 at 7:21
  • 1
    In future, please consider indenting your code before posting it. It will make spotting errors much easier. Commented Mar 25, 2012 at 7:23
  • I agree to both of your comments. I am just learning this up, so I thought I would write a basic frame which works and then get it fixed up against injections. About *, used it since that was easy than mentioning 3 fields, but will definitely do that. Still, I cant really find where my code goes wrong.. I have gone through the codes many times already. Commented Mar 25, 2012 at 7:23

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@Kishor - To confirm Michael's theory, what is the value that $name is set to at the end?
That fixed it. I will keep that in mind, and wont repeat that stuff again :) Thanks
@Kishor: I still on a weekly basis get errors from conditions like if($x = 1) instead of if($x == 1), so I'm betting you'll repeat all kinds of stuff again. You'll just get better at hitting yourself on the head when it does.
@Anthony, that's why it's worth doing if (1 == $x) as if (1 = $x) will result in a parse error. It's not as intuitive but it's less error-prone (or, rather, it's difficult to miss the errors if you have error reporting turned on).
@Anthony: in addition to Yoda conditions, another common idiom is to surround intentional assignments in conditions with additional parentheses: if (isset($foo) && ($foo = trim($foo))) {...}. Outside of Objective-C (and some linters), this won't generate any notices, but will inform any programmers not to change the assignment into an equivalence comparison.
|
1

I believe that the issues is that, at that point, there are no more rows left, as your while loop has fetched all of them.

If I'm not mistaken, this code:

while ($ignored = mysql_fetch_array($check)) {
    echo "Got a row! Rows left: " . mysql_num_rows($check);
}

Should output something like:

Got a row! Rows left: 3
Got a row! Rows left: 2
Got a row! Rows left: 1
Got a row! Rows left: 0

3 Comments

There will be only 1 row where email='$email' I checked the value of mysql_num_rows as $count just before matching it with 0 in the if statement. $count says 0, the if statement gets true as well.
@Kishor - Well, if it's not the while loop, then what happens if you change the condition to: if ($count == 0)?
Same thing happens. well figured out it was me missing the curly brackets under the last if statement. -_-
1

Following up on David's root-cause, here is a really simple fix:

$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)) {
        $name    = $data['name'];
        $license = $data['pid'];
        $comments = $data['comments'];
    }

    $license = ($license == "Blahblah") ? "This is a second level license" : $license;

} else {
    $name = "";
    $license = "...........";
    $comments = "Email doesnt exist in the database";
}

2 Comments

$license = ($license == "Blahblah") ? "This is a second level license" : $license; Seems a bit confusing as I am just learning php. Anyways it was me missing curly brackets under the last if statement.
Yeah, part of why you missed it, I'm thinking, is because you have picked up the bad habit of not closing single-line conditionals in curly brackets, which is valid, but very hard to read and can quickly lead to losing track of where your other conditions open and close. Follow Michael's advice and always use curly braces. The reason I changed that one line (to conditional syntax known as ternary) is because it often times helps clean up some of those spots where you just don't feel like having 3-5 lines of code to set 1 var.

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.