note 117125 deleted from pdo.construct by cmb
| From: | cmb@php.net | Date: | Mon, 28 Sep 2020 13:27:53 +0000 |
| Subject: | note 117125 deleted from pdo.construct by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215478@lists.php.net to get a copy of this message | ||
Note Submitter: info at salientdigital dot com
----
If you happen to create a dsn like this...
<?php
//wrong dsn
$db = new PDO('mysql:database=yourdb;host=127.0.0.1', 'user',
'password');
$stmt = $db->prepare('SELECT * FROM tbl WHERE id = :id');
$stmt->bindParam(':id', $user_id, PDO::PARAM_INT);
$result = $stmt->execute();
?>
... your result will be bool(false) and your $db->errorInfo() will be "0000" which
isn't very helpful.
You have to look to the $stmt->errorInfo() to see "No database selected" which is even
more confusing until you realize that it's not 'database' but 'dbname' in
your DSN! You would think the error about a bad DSN would be in the $db errorInfo but it is not,
it's not until you try the $stmt that the error is generated.
<?php
//correct dsn
$db = new PDO('mysql:dbname=yourdb;host=127.0.0.1', 'user',
'password');
?>
I wasted a half hour on that one.