I have tried out two different types of PDO connections, both of them are working fine on localhost, but only one of them work properly on a remote server even though they both use exactly the same credentials.
The first that works both on localhost and remotely is:
try {
$pdo = new PDO('mysql:host=xx.xxx.xxx.xx; dbname=dbxxxx', 'uxxxx', 'pxxxx');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
The second one that only works locally using exactly the same credentials as above:
echo "hello";
error_reporting(E_ALL);
ini_set("display_errors", 1);
$databaseHost = 'xx.xxx.xxx.xx';
$databaseName = 'dbxxxx';
$databaseUsername = 'uxxxx';
$databasePassword = 'pxxxx';
//$port="3306";
$charset = 'utf8';
try {
//$dsn = "mysql:host=$databaseHost;port=$port;dbname=$databaseName;charset=$charset";
$dsn = 'mysql:host=$databaseHost;dbname=$databaseName;charset=$charset';
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $databaseUsername, $databasePassword, $opt);
} catch(PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
So, I am puzzled why the top one works and not the bottom one.
-thanks