-2

我见过很多人有同样的问题,但没有一个解决方案是相关的/他们没有工作。

首先,这是我的简单代码,可以让事情变得简单:

<?php
$mysqli = new mysqli("localhost", "root", "", "pembsweb");
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
function mysqli_secure($string) {
        $string = htmlspecialchars($string);
        $string = trim($string);
        if (get_magic_quotes_gpc()) {
            $string = stripslashes($string);
        }
        $string = $mysqli->real_escape_string($string);
    return $string;
}
echo mysqli_secure("lets\\test//this");
?>

这会导致错误:

致命错误:在第 13 行调用非对象上的成员函数 real_escape_string()

我现在真的很愚蠢和烦躁.. 有人能帮帮我吗?

编辑:我已经尝试从函数中删除所有其他行,所以它所做的只是 mysqli 真正的转义,但仍然没有。我还尝试$string = new stdClass();在函数的第一行定义 $string,但仍然没有。

4

1 回答 1

2

$mysqli是在其他范围内定义的,而不是您尝试在其中使用它。将其作为参数传递。

<?php
$mysqli = new mysqli("localhost", "root", "", "pembsweb");
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
function mysqli_secure($mysqli, $string) {
        $string = htmlspecialchars($string);
        $string = trim($string);
        if (get_magic_quotes_gpc()) {
            $string = stripslashes($string);
        }
        $string = $mysqli->real_escape_string($string);
    return $string;
}
echo mysqli_secure($mysqli, "lets\\test//this");
?>
于 2013-07-05T15:11:04.770 回答