0

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

user2371684
  • 1,475
  • 5
  • 20
  • 45
  • It might be a permission issue (that the user can connect from localhost, but not outside). And you create the PDO object *outside* the `try/catch`, so `$e->getMessage()` doesn't give you an exact message. Put the code inside the `try` block, and it might thor you an exception which you can catch. Give us that message, and we'll have something to work with. ;-) – Qirel Apr 16 '17 at 10:35
  • I updated the code above, so now in the second option the PDO object is before the try catch. But I just get a empty page. But if its a permission issue, why can I connect with the first option and not second? – user2371684 Apr 16 '17 at 10:44
  • If you get an empty page its' either because there's a syntax error (check your logs if that's the case), or there simply might just be *no output*. If it connects properly, and that's all the code you have, there is nothing being printed. But enable error-reporting in any case; `error_reporting(E_ALL); ini_set("display_errors", 1);` at the top of your file, after ` – Qirel Apr 16 '17 at 10:52
  • Care to explain what do you mean under "doesn't work"? – Your Common Sense Apr 16 '17 at 10:52
  • Either way, there is no difference between these two. Both should work given provided with correct credentials. Whatever issue you can have is from a typographic error or the like. – Your Common Sense Apr 16 '17 at 10:54
  • First make sure that you are using the right "host", most of Hosting Providers such as Hostgator using localhost in the other hand, there are Hosting Providers using custom one, you may find it in your control panel. what is your Hosting Providers? and is it shared? – Moustafa Elkady Apr 16 '17 at 10:57
  • well, the second connection works and displays a call from a store procedure, the second one just displays a empty page, no errors. – user2371684 Apr 16 '17 at 10:58
  • I adde the error reporting at the top now, same result, empty page. The credentials are exactly the same for both connections. I even set a echo statement top of the page, and that doesn't show up either. If there is typo, I will try to create a new file and no copy/paste in between :) – user2371684 Apr 16 '17 at 11:04
  • the pdo error mode is set in the $opt and then used in the $pdo – user2371684 Apr 16 '17 at 11:10

0 Answers0