mysql_real_escape_string没有显示错误 [英] mysql_real_escape_string not showing error

查看:64
本文介绍了mysql_real_escape_string没有显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从Mysql扩展名移至PHP PDO. 我面临一个奇怪的问题. 在我的开发环境中,数据库服务器[MySQL]和Web服务器都位于单个系统中,而在测试环境中,Web服务器和数据库服务器都位于不同的系统中.

I am moving my application from Mysql extension to PHP PDO. I am facing a strange problem. In my development environment, I have both db server [MySQL] and web server are in single system where as in testing environment web and db servers are in different system.

以下测试代码可在开发环境中完美运行,而在测试环境中将失败.

The following test code runs perfectly in dev environment and fails in test environment.

    require "class.DB.php" ;

    class DbMaster extends DB
    {
        public function __construct()
        {
            $this->Host     = "192.168.1.00";
            $this->Database = 'test_database';
            $this->User     = "root";
            $this->Password = "12345";
        }
    }

    // Creates the instance
    $db = new DbMaster();

    $table = mysql_real_escape_string('persons');
    $result_array = $db->query("SELECT Id, Age FROM $table WHERE Id >= :Id", array("Id" => 1));

    foreach ($result_array as $rec)
    {
        echo '<br>'.$rec["Id"].' -> '.$rec['Age'];

    }

在开发中,mysql_real_escape_string应该失败,因为没有mysql_connect().

In dev, mysql_real_escape_string should fail, because there is no mysql_connect().

但是,当有一个本地运行的mysql服务器时,mysql_real_escape_string起作用.为了在开发环境中进行测试,我停止了本地mysql并连接到远程数据库.然后出现以下错误:

But, mysql_real_escape_string works when there is a mysql server running locally. To test this in dev environment I stopped the local mysql and connected to remote database. Then I got the following error:

Warning: mysql_real_escape_string(): A link to the server could not be established 

因此,使用我现有的开发设置(包括Web服务器和db服务器),我看不到与PDO相关的错误.

So with my existing development setup [both web and db server together], I am not able to see the PDO related errors.

任何解决此问题的方法.

Any way to resolve this problem.

推荐答案

mysql_real_escape_string()尝试使用mysql_connect()打开与服务器的连接. mysql_connect()的默认值为"localhost"和"root",没有密码.

mysql_real_escape_string() tries to open a connection to a server with mysql_connect() if no connection exist. The default values for mysql_connect() are "localhost" and "root" without a password.

如果您的开发环境中具有没有密码的root帐户(这是很常见的设置),则可以正常工作,因为可以建立连接.

If you have the root account without a password in your development environment (which is a pretty common setup) this will work without problem, since a connection can be esteblished.

另一方面,在实际环境中,root用户希望设置了密码,因此该调用将失败.

On the live environment on the other hand, the root user hopefully has a password set, so this call will fail.

在这种情况下,mysql_real_escape_string()将返回false而不是您的转义值.

In this case mysql_real_escape_string() will return false instead of your escaped value.

解决方案:使用功能的mysqli或PDO等效项.或暂时使用具有有效凭据的mysql_connect()打开其他连接,以便mysql_real_escape_string()可以使用它.

The solution: Use the mysqli or PDO equivalent of the function. Or open an additional connection using mysql_connect() with valid credentials for the time being so mysql_real_escape_string() can use it.

这篇关于mysql_real_escape_string没有显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆