-2

我花了一些时间在 Internet 上搜索并使用我的代码,但我仍然无法弄清楚为什么我会收到此错误消息。这是我的代码的摘录:

    } else {
        if (!empty($errors) && nexus_error($nexus)==false) {
            $message = "There were" . count($errors) . " errors in the form.";
        } if (!empty($errors) && nexus_error($nexus)) {
            $message = "There were" . count($errors) . " errors in the form.";
            $message .= "A user with the username" . $nexus . " already exists in the database."; 
        } if (empty($errors) && nexus_error($nexus)) { //***this line causes the error
            $message = "A user with the username" . $nexus . " already exists in the database."; 
        }
    }

顺便说一下,函数 nexus_error 定义如下:

function nexus_error($sel_nexus) {
    global $connection;
    $query = "SELECT * FROM person WHERE nexus={$sel_nexus}";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    if (count(mysql_fetch_array($result_set)) != 0) {
        return true;    // bad
    } else {
        return false;  
    }
}

任何帮助都会很棒。谢谢你的时间 :)

4

2 回答 2

2
if (count(mysql_fetch_array($result_set)) != 0)

你不能count()一个函数返回的值。您应该先将其存储在变量中。

于 2012-05-22T15:12:17.620 回答
0

正如萨米所说,有问题的行是if (count(mysql_fetch_array($result_set)) != 0) {

计算返回的结果数量的正确方法是 mysql_num_rows()而不是计算,您的行可能只是这样:

if (mysql_num_rows($result_set) != 0) {

此外,您的代码目前效率低下,因为如果它过滤到最后一条语句(即 2 个不必要的查询),nexus_error($nexus) 可以在同一个变量上调用 3 次,请考虑像这样进行重构:if

$nexusError = nexus_error($nexus);
 } else {
    if (!empty($errors) && $nexusError ==false) {
        $message = "There were" . count($errors) . " errors in the form.";
    } if (!empty($errors) && $nexusError) {
        $message = "There were" . count($errors) . " errors in the form.";
        $message .= "A user with the username" . $nexus . " already exists in the database."; 
    } if (empty($errors) && $nexusError) { //***this line causes the error
        $message = "A user with the username" . $nexus . " already exists in the database."; 
    }
}
于 2012-05-22T15:15:56.853 回答