与MySQL数据库的PDO连接被拒绝 [英] PDO connection to MySQL database refused

查看:268
本文介绍了与MySQL数据库的PDO连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到我的NearlyFreeSpeech MySQL数据库.我可以通过PHPMyAdmin登录,但不能通过PDO登录.我正在使用此代码

I am trying to connect to my NearlyFreeSpeech MySQL database. I can login through PHPMyAdmin but not through PDO. I am using this code

 $dbconn = new PDO('mysql:host=127.0.0.1;dbname='.$config['db'].'; port=3307', $config['user'], $config['pass']);

$ config在单独的文件中定义的位置.我得到的错误是:

Where $config is defined in a separate file. The error I get is:

Warning: PDO::__construct() [pdo.--construct]: [2002] Connection refused (trying to connect via tcp://127.0.0.1:3307) 
Error: SQLSTATE[HY000] [2002] Connection refused

然后最终

Fatal error: Call to a member function query() on a non-object in...

如果我使用

mysql:host=localhost

我得到的错误是

Error: SQLSTATE[HY000] [2002] No such file or directory

现在,我认为连接被拒绝"比没有这样的文件或目录"要好,但是我不知道从这里去哪里.知道为什么会这样吗?谢谢您的帮助.

Now I assume "Connection refused" is better than "No such file or directory", but I don't know where to go from here. Any idea why this is happening? Thank you for your help.

推荐答案

尝试使用我现有的函数和常量变量,也可以将这些常量更改为您拥有的数组变量.

Try my existing functions and constant variables, you may also change those constant to an array variable you have.

define("SERVER_SQL_VERSION","mysql");
define("SQL_SERVER","localhost");
define("SQL_PORT","3306");
define("SQL_USERNAME","root");
define("SQL_PASSWORD","");
define("SQL_DB_NAME","db");

if(!function_exists('pdoConnect')) {
    function pdoConnect() {
        $pdo = new PDO(SERVER_SQL_VERSION.":host=".SQL_SERVER.";dbname=".SQL_DB_NAME."", "".SQL_USERNAME."", "".SQL_PASSWORD.""); 
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        return $pdo;
    }
}

关于您的连接可能存在问题,该问题一定可以解决...

There might be an issue about your concatenation, this must be working...

我还分离了SERVER_SQL_VERSION,并添加了一个功能来检查驱动程序是否可用...我正在使用XAMPP软件,并且如果您/其他人尝试使用postgresql等,则只有mysql和sqlite是活动的. .那一定也能正常工作.

I also seperated the SERVER_SQL_VERSION, and added a functions to check if the driver is available...I am using the XAMPP software and only mysql and sqlite is active, if you/others try to use postgresql and so on...that must be working as well.

if(!function_exists('check_sql_version')) {
    function check_sql_version() {
        $sql_available = false;     //make it false yet
        foreach(PDO::getAvailableDrivers() as $key => $val) {
            if(SERVER_SQL_VERSION == $val)
            {
                $sql_available = true;
            }
        }
        //check now if sql_available is true or false
        if($sql_available == true)
            return true;
        else
            return false;
    }
}

因此应考虑一个样本:

if(!check_sql_version()) {
    echo '('.SERVER_SQL_VERSION.') is not available, you only have this drivers:<br/>';
    foreach(PDO::getAvailableDrivers() as $key => $val) {
        $key = $key + 1;
        echo $key.') '.$val.'<br/>';
    }
    exit(); //exit and dont proceed
}
$stmt = pdoConnect()->prepare("SELECT * FROM accounts");
$stmt->execute();

希望对您有帮助!

这篇关于与MySQL数据库的PDO连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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