简体   繁体   English

满足mysqli_real_escape_string参数错误的正确结构

[英]Proper Structure to Satisfy mysqli_real_escape_string parameters error

I am having a structure issue with implementing parameters for a `mysqli_real_escape_string. 我在为`mysqli_real_escape_string实现参数时遇到结构性问题。 The place I'm using in is within a function like such: 我正在使用的位置在这样的函数中:

    /**
     * Clean the array using mysql_real_escape_string
     *
     * Cleans an array by array mapping mysql_real_escape_string
     * onto every item in the array.
     *
     * @param array $array The array to be cleaned
     * @return array $array The cleaned array
     */

    function clean($array) 
    {
        return array_map('mysqli_real_escape_string', $array);
    }

I get a double warning of this: 我对此有双重警告:

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\\vhosts\\goodgirls1\\core\\database\\db.php on line 59 警告:mysqli_real_escape_string()恰好需要2个参数,第59行的C:\\ vhosts \\ goodgirls1 \\ core \\ database \\ db.php中给出了1个参数

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\\vhosts\\goodgirls1\\core\\database\\db.php on line 59 警告:mysqli_real_escape_string()恰好需要2个参数,第59行的C:\\ vhosts \\ goodgirls1 \\ core \\ database \\ db.php中给出了1个参数

So, I tried this instead to see if I could satisfy the error: 因此,我改用此方法查看是否可以满足该错误:

function clean($array) {
        return array_map(mysqli_real_escape_string(mysqli_connect('localhost', DB_USER, DB_PASS)), $array);
    }

It seems to have satisfied it, but now I get this warning: 似乎已经满意了,但是现在我得到了这个警告:

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\\vhosts\\goodgirls1\\core\\database\\db.php on line 59 警告:mysqli_real_escape_string()恰好需要2个参数,第59行的C:\\ vhosts \\ goodgirls1 \\ core \\ database \\ db.php中给出了1个参数

And this is where I'm now lost. 这就是我现在迷路的地方。 What do I give mysqli_real_escape_string to make it happy? 我怎样赋予mysqli_real_escape_string使其快乐? If I give it an incorrect missing parameter, then my array seems to blow up. 如果我给它一个不正确的缺少参数,那么我的数组似乎崩溃了。 I would appreciate some advice on how to proceed from here. 我希望您能从此处开始提出一些建议。 Thanks! 谢谢!

function clean($array) {
    $connection = mysqli_connect('localhost', DB_USER, DB_PASS);
    return array_map(
        function($value) use ($connection) {
            return mysqli_real_escape_string($connection, $value);
        },
        $array
    );
}

But (as you're already using MySQLi) consider using prepared statements/bind variables instead 但是(因为您已经在使用MySQLi),请考虑改用准备好的语句/绑定变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM