【问题标题】:codeIgniter use mysql_real_escape_string() instead.database connection issuecodeIgniter 使用 mysql_real_escape_string() 代替。数据库连接问题
【发布时间】:2014-11-27 22:50:28
【问题描述】:

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

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

【问题讨论】:

  • 您是否在代码中的任何位置使用该函数?您在此处发布的内容与此无关。
  • @tadman 我更新了这个问题,实际上我是这个主题的新手我必须运行这个项目我得到了这个错误
  • 它似乎正在退回到某种兼容模式,因为它找不到mysqli。它加载了哪些模块?您通常可以通过phpinfo() 查找。
  • @tadman 我不知道如何找到它
  • 顺便说一句,它可以帮助您拥有一个 config/staging 和 config/development 文件夹,您可以在其中添加自定义的 database.php 文件。然后可以从 config/database.php 文件中包含公共数据库。

标签: php mysql codeigniter mysqli mamp


【解决方案1】:

在 Model 类中创建一个函数,该函数将使用数据库连接字符串并从控制器传递值以使用 mysqli_real_escape_string() 进行转换

function MysqliRealeScapeString($val){

    $new = mysqli_real_escape_string($this->db->conn_id, $val);
    return $new;
}

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:

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

      class Base_controller extends CI_Controller {
       function __construct() {
           parent::__construct();
      
          $this->load->database('db_name');
          $this->db->initialize();
      

      【讨论】:

        【解决方案4】:

        另外,要使用函数 mysqli_real_escape_str,你需要 mysqli 来获取它

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

        上面的函数返回一个mysqli对象,这样你就可以使用了,

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

        效果很好,不过在这里没那么重要!

        【讨论】:

          【解决方案5】:

          不要害怕更改核心文件,只需更改 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;
          }
          

          我遇到了同样的问题


          更好的解决方案 -> 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"

          【讨论】:

          • CodeIgniter 2.2.0于今年6月发布。 GitHub repo 有很多活动。也许它对你来说已经死了,但它并没有死。
          • 如何设置 ENVIRONMENT 常量(在 index.php 中)?
          • @Gervs 对于一个死气沉沉的平台,令人惊讶的是 3.0 处于测试阶段并将在今年晚些时候完全发布。仅供参考,修改core 文件绝不是要走的路。 A.) 当您遇到此类问题时,可能会有受支持的更新(如此处所示)。 B.) 这是application 目录中文件夹背后的全部原因。您在那里进行扩展修改并合并。
          【解决方案6】:

          试试这个:

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

          我已经更新了答案

          【讨论】:

          • 然后我有这个错误,PDOException [1049]: SQLSTATE[42000] [1049] Unknown database 'bmgr1'
          • copying the project folder inside htdocs 到你的机器上几乎肯定没有带 MySQL 数据库;您需要从生产机器导出数据库,然后将其导入本地 MySQL 实例。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-04
          • 2016-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-13
          相关资源
          最近更新 更多