【问题标题】:Proper Structure to Satisfy mysqli_real_escape_string parameters error满足 mysqli_real_escape_string 参数错误的正确结构
【发布时间】:2014-12-30 12:20:03
【问题描述】:

我在为 `mysqli_real_escape_string 实现参数时遇到了结构问题。我正在使用的地方是在这样的函数中:

    /**
     * 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);
    }

我收到了双重警告:

警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:\vhosts\goodgirls1\core\database\db.php 第 59 行给出

警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:\vhosts\goodgirls1\core\database\db.php 第 59 行给出

所以,我尝试了这个,看看我是否可以满足错误:

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

它似乎已经满足了,但现在我收到了这个警告:

警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:\vhosts\goodgirls1\core\database\db.php 第 59 行给出

这就是我现在迷路的地方。我给mysqli_real_escape_string 什么让它开心?如果我给它一个不正确的缺失参数,那么我的数组似乎会爆炸。我会很感激一些关于如何从这里开始的建议。谢谢!

【问题讨论】:

  • 您应该查看手册。

标签: php arrays mysqli parameter-passing


【解决方案1】:
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
    );
}

但是(因为您已经在使用 MySQLi)考虑使用准备好的语句/绑定变量

【讨论】:

  • 谢谢,我想不通这个世界。错误已删除!
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多