错误:“mysqli_real_escape_string()期望参数1为mysqli,null为”

时间:2014-04-30 02:06:26

标签: php mysql mysqli

当我尝试使用下面的功能时出现错误。

require_once("connection.php");

function check_max_field_lengths($field_length_array) {
    $field_errors = array();
    foreach($field_length_array as $fieldname => $maxlength ) {
        if (strlen(trim(mysqli_real_escape_string($connection, $_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname;
        }
    }
    return $field_errors;
}

它说:

mysqli_real_escape_string() expects parameter 1 to be mysqli, null given

但我已经在connection.php文件中定义了$connection,如下所示:

require("constants.php");
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME) or die("Database connection failed: " . mysqli_error($connection));

$ connection在我的其他用途中工作正常,但不在这里。 知道为什么吗?

1 个答案:

答案 0 :(得分:2)

只需将连接传递给您的函数

即可
function check_max_field_lengths($field_length_array, $connection) {