1

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

我尝试使用这些自己调试问题。

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

对此有什么帮助吗?

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

3 回答 3

3

你的意思肯定是这样的:

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总是设置为...........

始终使用大括号。

于 2012-03-25T07:33:57.557 回答
1

我认为问题在于,那时已经没有更多的行了,因为您的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
于 2012-03-25T07:26:54.237 回答
1

跟进大卫的根本原因,这是一个非常简单的修复:

$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";
}
于 2012-03-25T07:36:03.917 回答