mysql_num_rows在if语句中



我面临的问题是,mysql_num_rows在整个代码中都给了我1的输出,但当我在if语句中匹配它wil 0时,它会返回true并执行代码。所以$license返回。。。。。。。。而不是其实际值。

我试着用这些来调试这个问题。

  • 尝试打印以查看数据是否存在。-是的
  • 尝试在第一部分回显$license-返回正确的值
  • 尝试检查mysql_num_rows的值-返回1
  • 在if语句中将其与0匹配-由于值为1,因此当它应该为false时返回true

有什么帮助吗?

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

你的意思肯定是:

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

而不是

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

不使用大括号意味着只有if语句下面的第一行包含在其中。因此$license总是设置为...........

始终使用花括号

我认为问题是,在这一点上,已经没有更多的行了,因为您的while循环已经获取了所有的行。

如果我没有错的话,这个代码:

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

应该输出以下内容:

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

根据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";
}

最新更新