-2

我正在运行以下查询:

$cv_specs = select("SELECT * FROM file_details WHERE client_details_id='$cv_specs_id'");

通过函数选择

function select($query) {
        $mysqli = connect();

        $result = $mysqli->query($query);

        while ( $obj = $result->fetch_object() ) {
            $return[] = $obj;
        }

        return $return;
    }

只要返回结果,这一切都可以正常工作。但是,如果没有找到结果,则它包含在错误中的页面如下:

Notice: Undefined variable: return

如果没有结果,我得到 $return 未在函数中定义,但如果没有结果,则无法尝试更新函数以设置不同的变量。

4

2 回答 2

3
    $return = array();
    while ( $obj = $result->fetch_object() ) {
        $return[] = $obj;
    }

    return $return;

Define it first.

于 2013-06-17T18:04:31.420 回答
1

Define the variable: $return = array(); Problem solved.

But I really hope you're not creating a new database connection every single time you want to select data, and never closing any of those connections...

于 2013-06-17T18:05:00.980 回答