致命错误:在写上下文中不能使用函数返回值

时间:2012-05-22 15:09:15

标签: php mysql syntax-error fatal-error

我花了一些时间在互联网上搜索并玩弄我的代码,但我仍然无法弄清楚为什么我收到此错误消息。 以下是我的代码的摘录:

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

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

2 个答案:

答案 0 :(得分:2)

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

你不能count()函数返回值。你应该在之前将它存储在一个变量中。

答案 1 :(得分:0)

正如萨米所说的那条线是if (count(mysql_fetch_array($result_set)) != 0) {

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

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

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

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