mysql_real_escape_string 在 cakephp 中不工作

mysql_real_escape_string is not working in cakephp

mysql_real_escape_string 在 cakephp 中不起作用。 我收到如下错误。 mysql_real_escape_string():用户 'root'@'localhost' 的访问被拒绝(使用密码:NO)[APP/Controller/add.php,第 123 行] 数据库连接:

  <?php

    class DATABASE_CONFIG {

        public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'xyz',
            'password' => 'password',
            'database' => 'xyz',
            'prefix' => '',
            //'encoding' => 'utf8',
        );

        public $test = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'xyz',
            'password' => 'password',
            'database' => 'xyz',
            'prefix' => '',
            //'encoding' => 'utf8',
        );
    }
     ?>




 <?php 
          $price1=implode("'~'",array_map('mysql_real_escape_string',$this->request->data['iupdate']['price']));
    ?>

本地主机工作正常,但在服务器中出现错误。

我不知道蛋糕 php,但是恕我直言,你不能使用 mysql_real_escape_string,因为:

  • 首先,已弃用。

    This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

  • 其次,根据php doc

    The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.

因此,您正在尝试通过 php.ini 中设置的值连接到您的生产服务器,当然,出于安全原因,您不能使用 root 权限连接。

正如我告诉你的:我不知道 cakePHP,但我很确定,有一个函数可以转义字符串,或者 - 更好 - 使用 PDO prepare 语句自动转义字符串

如果您在安装 cakephp 2.0 及更多版本时发现此问题,请将 'login' => 'xyz' 替换为 'username' => 'xyz'.

简单的技巧即可解决所有问题。