Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO: Unable to connect, Invalid catalog name

I am trying to set up a new site on my hosting (Host route if it matters) but i keep getting this error when i try using PDO (first PDO site im trying):

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /home/kennyi81/public_html/gamersite/login.php:36 Stack trace: #0 /home/kennyi81/public_html/gamersite/login.php(36): PDOStatement->execute() #1 {main} thrown in /home/kennyi81/public_html/gamersite/login.php on line 36

When i use these settings:

$dbh = new PDO("mysql:91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

....

$stmt = $dbh->prepare('SELECT * FROM USERS WHERE ID = :id LIMIT 1');

How the database is laid out:

enter image description here

I am able to use mysqli connect fine on my other sub domains / main site, but i just cannot get PDO to work.

I've tried this, which i have seen around:

 $stmt = $dbh->prepare('SELECT * FROM gamersite.USERS WHERE ID = :id LIMIT 1');

but it retuns a syntax error.

Anyone have any idea what may be causing this?


This is all working on my local server, nothing changed on upload apart from connect line.

like image 378
Tristan Cunningham Avatar asked Aug 28 '13 22:08

Tristan Cunningham


1 Answers

Instead of:

$dbh = new PDO("mysql:91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");

Try:

$dbh = new PDO("mysql:host=91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");

(add host=)

And it most likely works on your local server, because you have mysql:localhost... or mysql:127.0.0.1... there and it's ignored (cause it's missing host= aswell) and by default it's localhost.

like image 87
Michał Prajsnar Avatar answered Nov 15 '22 15:11

Michał Prajsnar