php codeIgniter 使用 mysql_real_escape_string() 代替.database 连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26169455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:12:29  来源:igfitidea点击:

codeIgniter use mysql_real_escape_string() instead.database connection issue

phpmysqlcodeignitermysqlimamp

提问by vishu

I have code igniter installed on server with database I want to run the same db on my mac, I used MAMP and I copy the project folder inside htdocs, but I have this error would you please help me!

我在带有数据库的服务器上安装了代码点火器我想在我的 mac 上运行相同的数据库,我使用了 MAMP 并复制了 htdocs 中的项目文件夹,但是我遇到了这个错误,请你帮帮我!

ErrorException [ 8192 ]: mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead.

回答by Gervs

Don't be afraid to change core files, just alter FCPATH/system/database/drivers/mysqli/mysqli_driver.php

不要害怕更改核心文件,只需更改 FCPATH/system/database/drivers/mysqli/mysqli_driver.php

function escape_str($str, $like = FALSE)
{
    if (is_array($str))
    {
        foreach ($str as $key => $val)
        {
            $str[$key] = $this->escape_str($val, $like);
        }

        return $str;
    }

    if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
    {
        $str = mysqli_real_escape_string($this->conn_id, $str);
    }
    else
    {
        $str = addslashes($str);
    }

    // escape LIKE condition wildcards
    if ($like === TRUE)
    {
        $str = str_replace(array('%', '_'), array('\%', '\_'), $str);
    }

    return $str;
}

I had the same issue

我遇到过同样的问题



Better solution -> https://ellislab.com/forums/viewthread/228288/"stated in github that it will be fixed in CodeIgniter 3.0 the fix already exists in that repository"

更好的解决方案-> https://ellislab.com/forums/viewthread/228288/"stated in github that it will be fixed in CodeIgniter 3.0 the fix already exists in that repository"

回答by vishu

Try this

尝试这个

function escapeString($val) {
    $db = get_instance()->db->conn_id;
    $val = mysqli_real_escape_string($db, $val);
    return $val;
}

回答by Salvo t.

try this:

尝试这个:

$db['development']['hostname'] = 'mysql:host=localhost';
$db['development']['dbdriver'] = 'pdo';

$db['staging']['hostname'] = 'mysql:host=localhost';
$db['staging']['dbdriver'] = 'pdo';

I have update the answer

我已经更新了答案

回答by Cosmo Arun

also, to use the function mysqli_real_escape_str, you need the mysqli , to get it

此外,要使用函数 mysqli_real_escape_str,您需要 mysqli 来获取它

function get_mysqli() { 
$db = (array)get_instance()->db;
return mysqli_connect('localhost', $db['username'], $db['password'], $db['database']);}

The above function returns a mysqli object such that you can use,

上面的函数返回一个 mysqli 对象,以便您可以使用,

$escaped = mysqli_real_escape_string(get_mysqli(),$string);

It works well, not so important here though!

它运作良好,但在这里并不重要!

回答by Andrey Chernov

It's not goot idea edit core CI files. If you don't want see deprecated warnings from mysql_escape_string, to use mysql_real_escape_string()instead you need open connection with DB. Use db->initialise()in your base controller

编辑核心 CI 文件不是个好主意。如果您不想看到来自 的已弃用警告mysql_escape_string,而要使用,mysql_real_escape_string()则需要打开与 DB 的连接。使用db->initialise()在你的基地控制器

class Base_controller extends CI_Controller {
 function __construct() {
     parent::__construct();

    $this->load->database('db_name');
    $this->db->initialize();