Not able to connect to MySQL server using PDO [clo

2019-02-21 16:03发布

问题:

I have a PHP script which I use to connect to a MySQL database. Connection through mysql_connect works perfectly, but when trying with PDO I get the following error:

SQLSTATE[HY000] [2005] Unknown MySQL server host 'hostname' (3)

the code I use to connect is below:

    <?php  
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    $hostname_localhost ="hostname";  
    $database_localhost ="dbname";  
    $username_localhost ="user";  
    $password_localhost ="pass";  
    $user = $_GET['user'];  
    $pass = $_GET['pass'];

    try{
        $dbh = new PDO("mysql:host=$hostname_localhost;dbname=$database_localhost",$username_localhost,$password_localhost);
        echo 'Connected to DB';
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $dbh->prepare("SELECT check_user_company(:user,:pass)");
        $stmt = $dbh->bindParam(':user',$user,PDO::PARAM_STR, 16);
        $stmt = $dbh->bindParam(':pass',$pass,PDO::PARAM_STR, 32);

        $stmt->execute();

        $result = $stmt->fetchAll();

        foreach($result as $row)
        {
            echo $row['company_id'].'<br />';
        }

        $dbh = null;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    } 
    ?>  

Thanks in advance

回答1:

Got the same problem. Mine solution was another database port. I wrote localhost:1234 and got this error.

Fixed with:

mysql:host=$hostname_localhost;port=1234;dbname=$database_localhost",$username_localhost,$password_localhost);
        echo 'Connected to DB';


回答2:

It does seem pretty straightforward, here is what I use to build my PDO connectors(noticed your dbname and host are done differently than mine, dunno if that's relevant, but worth a check):

PDO Creation function

require_once('config.inc.php');

function buildDBConnector(){
    $dsn = 'mysql:dbname='.C_BASE.';host='.C_HOST;
    $dbh = new PDO($dsn, C_USER, C_PASS);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

config.inc.php

define('C_HOST','localhost');// MySQL host name (usually:localhost)
define('C_USER','sweetUsername');// MySQL username
define('C_PASS','sweetPassword');// MySQL password
define('C_BASE','superGreatDatabase');// MySQL database

And while it makes no sense, when I tried to declare $dsn inline including variables during the newPDO call, I kept getting failures too. I broke it apart and used the $dsn variable to do so. And haven't had an issue since.

Wondering if you're in shared hosting by chance?

NOTE: If you don't have a dedicated IP, and instead are going through a NAT, your IP won't properly translate to your actual server.

That help at all?

UPDATE: Just thought of another thing. Are you trying to connect to a mysql database that is on a different IP than you are running your scripts from? If so, you will likely need to enable remoteSQL access for the ip you are calling the database from. Fairly easy to do, but CRITICAL if you are not accessing localhost.



回答3:

You dont seem to specify the database host dns details or IP address. Adding that will solve the problem