使用MySQLi时,在非对象上调用成员函数real_escape_string()

inv*_*ous 1 php mysql mysqli

我已尝试在关于此主题的其他问题上发布的每个解决方案,但它们都不起作用.

我很抱歉,如果这是一个基本问题,但我是MySQLi的新手,我无法弄清楚为什么这个连接不起作用.

我的functions.php文件中有一个函数,它具有:

function cleanInput($string) {
    $stripsql = $connect->real_escape_string($string);
    $addslashes = addslashes($stripsql);
    return $addslashes;
}
Run Code Online (Sandbox Code Playgroud)

第二行是在标题中抛出该错误.

我连接到数据库,$connect是一个工作变量.它在config.php中设置使用:

$connect = new mysqli('localhost', 'shop', 'password', 'zen');
Run Code Online (Sandbox Code Playgroud)

我甚至尝试在功能之前移动该线路无济于事.

var_dump$connect在functions.php文件不返回NULL,则返回有关数据库开始的数据object(mysqli)#1 (19) { ["affected_rows"]=> int(0),然后用大量的行继续.

Kev*_*vin 7

目前,$connect超出范围,只需将其添加到参数中:

$connect = new mysqli('localhost', 'shop', 'password', 'zen');
function cleanInput($connect, $string = '') {
    if(!empty($string)) {
        $stripsql = $connect->real_escape_string($string);
        return $stripsql;
    }
}
$my_string = 'your string here';
$escaped_string = cleanInput($connect, $my_string);
Run Code Online (Sandbox Code Playgroud)

旁注:或者如果可以,您也可以使用预准备语句.