Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误:";mysqli_real_escape_string()期望参数1为mysqli,给定为null;_Php_Mysql_Mysqli - Fatal编程技术网

Php 错误:";mysqli_real_escape_string()期望参数1为mysqli,给定为null;

Php 错误:";mysqli_real_escape_string()期望参数1为mysqli,给定为null;,php,mysql,mysqli,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($

我尝试使用下面的函数时出错

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在我的其他用途中可以正常工作,但在这里不行。
知道为什么吗?

只需将连接传递给函数即可

function check_max_field_lengths($field_length_array, $connection) {

... 答案并不是那么简单。新手错误。谢谢