Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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()_Php_Mysql_Mysqli - Fatal编程技术网

Php 使用MySQLi时对非对象调用成员函数real_escape_string()

Php 使用MySQLi时对非对象调用成员函数real_escape_string(),php,mysql,mysqli,Php,Mysql,Mysqli,我已经尝试了关于这个主题的其他问题的所有解决方案,但没有一个有效。 如果这是一个基本问题,我很抱歉,但我是MySQLi的新手,我不明白为什么这个连接不起作用 我的functions.php文件中有一个函数: function cleanInput($string) { $stripsql = $connect->real_escape_string($string); $addslashes = addslashes($stripsql); return $adds

我已经尝试了关于这个主题的其他问题的所有解决方案,但没有一个有效。

如果这是一个基本问题,我很抱歉,但我是MySQLi的新手,我不明白为什么这个连接不起作用

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

function cleanInput($string) {
    $stripsql = $connect->real_escape_string($string);
    $addslashes = addslashes($stripsql);
    return $addslashes;
}
第二行是在标题中抛出该错误

我确实连接到数据库,
$connect
是一个工作变量。它是在config.php中使用以下命令设置的:

$connect = new mysqli('localhost', 'shop', 'password', 'zen');
我甚至试着在函数之前移动那条线,但都没有用


functions.php文件中
$connect
上的
var\u dump
不会返回NULL,它返回有关数据库的数据,从
object(mysqli)#1(19){[“受影响的行”]=>int(0)
,然后继续大量行。

目前,
$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);

旁注:如果可以,也可以使用准备好的语句。

当前,
$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);

旁注:如果可以,也可以使用预先准备好的语句。

是的,范围问题。在函数良好中,全局不太好,许多现代开发人员都不喜欢。是的,范围问题。在函数良好中,全局不太好,许多现代开发人员都不喜欢。您的代码和添加
global$connect有什么区别;
在函数中?避免使用
全局
,应通过函数构造函数或对象属性传递变量(如果使用OO).进一步阅读:和…和
&
@invidious@invidiousdeceze给出了一个非常好且雄辩的答案,这应该会给你一个非常好的洞察力,我现在得到了
警告:cleanInput()缺少参数2,在
中调用。您的代码与在函数中添加
global$connect;
有什么区别?避免使用
global
,应通过函数构造函数或对象属性传递变量(如果使用OO).进一步阅读:和…和
&
@invidious@invidious这里是deceze的一个非常好且雄辩的回答,这应该会给你一个非常好的洞察力,我现在得到了
警告:cleanInput()缺少参数2,在
中调用。