简体   繁体   English

一条if语句中的mysql_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. 我面临的问题是,mysql_num_rows在整个代码中给我的输出为1,但是当我在if语句中将其匹配为0时,它将返回true并执行代码。 so $license returns ........ instead of its actual value. 因此$ license会传回........而不是其实际价值。

I tried to debug the problem myself using these. 我试图自己使用这些来调试问题。

  • Tried print_r to see if datas exists. 尝试使用print_r查看数据是否存在。 - Yes. -是的
  • Tried echoing the $license at first part - returns the right value. 尝试在第一部分回显$ license-返回正确的值。
  • Tried checking the value of mysql_num_rows - returns 1. 尝试检查mysql_num_rows的值-返回1。
  • Matching it with 0 in an if statement - returns true when it should be false since the value is 1. 在if语句中将其与0匹配-由于值为1,因此应为false时返回true。

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";

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. 不使用大括号意味着仅if语句下面的第一行包含在其中。 So $license is always set to ........... . 因此, $license始终设置为...........

Always use curly brackets. 始终使用大括号。

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. 我认为问题在于,此时,没有多余的行了,因为您的while循环已获取所有这些行。

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

Following up on David's root-cause, here is a really simple fix: 跟进David的根本原因,这是一个非常简单的解决方法:

$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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM