Reputation:
I've got the following code:
try {
$db = new PDO('mysql:host=127.0.0.1;db=example', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->query('SELECT * FROM posts');
} catch (PDOException $e) {
echo $db->errorCode();
var_dump($db->errorInfo());
}
And I get the following error:
3D000array(3) { [0]=> string(5) "3D000" [1]=> int(1046) [2]=> string(20) "No database selected" }
However, this ONLY occurs when I include $stmt = $db->query('SELECT * FROM posts');
This is my first time with PDO, Would anyone know why this is not working? The database and table exist.
Upvotes: 1
Views: 99
Reputation: 486
dname and not only db
try {
$db = new PDO('mysql:host=127.0.0.1;dbname=example', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->query('SELECT * FROM posts');
} catch (PDOException $e) {
echo $db->errorCode();
var_dump($db->errorInfo());
}
http://www.php.net/manual/fr/book.pdo.php
Upvotes: 0
Reputation: 146450
You have:
db=example
You need:
dbname=example
Sadly, there's no way to get an error message or notification of any kind if you mistype a DSN parameter.
Available parameters are documented at PDO_MYSQL DSN.
Upvotes: 4