使用MySQLi时在非对象上调用成员函数real_escape_string() [英] Call to a member function real_escape_string() on a non-object when using MySQLi

查看:86
本文介绍了使用MySQLi时在非对象上调用成员函数real_escape_string()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试过发布有关此主题的其他问题的所有解决方案,但都无济于事.

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

I'm sorry if this is a basic question, but I'm new with MySQLi and I can't figure out why this connection won't work.

我的functions.php文件中有一个具有以下功能的函数:

I have a function in my functions.php file that has:

function cleanInput($string) {
    $stripsql = $connect->real_escape_string($string);
    $addslashes = addslashes($stripsql);
    return $addslashes;
}

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

The second line is throwing that error in the title.

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

I do connect to the database, and $connect is a working variable. It's set in config.php using:

$connect = new mysqli('localhost', 'shop', 'password', 'zen');

我什至尝试过在该功能之前将该行移到无济于事.

I've even tried moving that line right before the function to no avail.

functions.php文件中$connect上的var_dump不返回NULL,它返回有关以object(mysqli)#1 (19) { ["affected_rows"]=> int(0)开头,然后以许多行继续的数据库的数据.

A var_dump on $connect in the functions.php file does not return NULL, it returns data about the database starting with object(mysqli)#1 (19) { ["affected_rows"]=> int(0), and then continuing with lots of rows.

推荐答案

当前,$connect不在范围内,只需将其添加到参数中即可:

Currently, $connect is out of scope, just add it in the parameter:

$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);

旁注:或者,如果可以的话,也可以使用准备好的语句.

Sidenote: Or if you can, you can also use prepared statements.

这篇关于使用MySQLi时在非对象上调用成员函数real_escape_string()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆