致命错误:无法在写入上下文中使用函数返回值


Fatal error: Can't use function return value in write context

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

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

任何帮助都会很棒。感谢您抽出宝贵时间:)

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

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

正如

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

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

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

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

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