简体   繁体   中英

mysql_connect() works PDO doesn't

I'm trying to connect to my database using PDO but i can't get it working. I get a 500 server error

MySQL version 5.0.83

'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known' inerror: Fatal error: Uncaught exception

Other error i get:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Operation timed out'

I changed the values in this example obviously. And yes they are the correct credentials. I can connect with Sequel Pro or other methods.

connection.php:

<?php

//Our MySQL user account.
define('MYSQL_USER', 'user');

//Our MySQL password.
define('MYSQL_PASSWORD', 'pass');

//The server that MySQL is located on.
define('MYSQL_HOST', 'host');

//The name of our database.
define('MYSQL_DATABASE', 'database');

/**
 * PDO options / configuration details.
 * I'm going to set the error mode to "Exceptions".
 * I'm also going to turn off emulated prepared statements.
 */
$pdoOptions = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

/**
 * Connect to MySQL and instantiate the PDO object.
 */
$pdo = new PDO(
    "mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, //DSN
    MYSQL_USER, //Username
    MYSQL_PASSWORD, //Password
    $pdoOptions //Options
);

Registration.php

<?php 

require 'connection.php';

The strange thing is that on an older page of this website mysql_connect() is used.

$db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db);

This works perfect with the exact same credentials.

It's hosted on a shared hosting and PDO is enabled on the server. Shared Hosting: Blacknight

I tried:

  • Not using $pdoOptions
  • Not using define()
  • Not using require and putting the new pdo code straight into the registration.php file
  • running the script in the answers and now my mysql_connect() does a time-out and on my hosting i get a whitescreen.

now: Fri, 15 Jan 2016 08:54:21 +0100 last changed:Fri, 15 Jan 2016 08:53:01 +0100 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in connection.php on line 14

Warning: mysql_connect(): Operation timed out in connectie.php on line 14 connection failed. Operation timed out

You said using the mysql_* functions the connection can be established, yet with he exact same parameters the PDO connection fails.
And it fails with SQLSTATE[HY000] [2002] php_network_getaddresses ....

So, abusing the "answers" section once again, I would like you to run the following script (only adjusting the defines) which will try both APIs.
It will also display the filemtime of the script; wouldn't be the first time some caching problem on a shared host caused the trouble ;-)

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
// for testing purposes keep everything in one file
define('MYSQL_USER', 'localonly');
define('MYSQL_PASSWORD', 'localonly');
define('MYSQL_HOST', 'localhost');
define('MYSQL_DATABASE', 'test');

echo 'now: ', date('r'), ' last changed:', date('r', filemtime(__FILE__));


$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD);
if ( !$db ) {
    die( 'connect failed. '.mysql_error() );
}
if ( !mysql_select_db(MYSQL_DATABASE, $db) ) {
    die( 'select_db failed. '.mysql_error($db) );
}
echo 'mysql: ', mysql_get_server_info($db), "<br />\r\n";


$pdoOptions = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

try {
    $dsn = "mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE;

    $pdo = new PDO(
        $dsn,
        MYSQL_USER, //Username
        MYSQL_PASSWORD, //Password
        $pdoOptions //Options
    );
    echo 'pdo: ', $pdo->getAttribute(PDO::ATTR_SERVER_INFO), "<br />\r\n";
}
catch(Exception $ex) {
    echo 'PDO failed.', $ex->getMessage();
    var_dump($dsn);
    die;
}

Does the mysql_* part of the script get executed (printing the server version) while the PDO part fails again?

It appears to be the hosting. I put the exact same script on a different shared hosting : 'Combell' , i made a database on that hosting , i changed the credentials and the script works like a charm. So the problem is 'Blacknight'. Thanks for the help everybody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM