note 117125 added to pdo.construct

From: Date: Sat, 18 Apr 2015 03:24:39 +0000
Subject: note 117125 added to pdo.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-202293@lists.php.net to get a copy of this message
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. ---- Server IP: 72.52.91.14 Probable Submitter: 72.201.206.159 ---- Manual Page -- http://php.net/manual/en/pdo.construct.php Edit -- https://master.php.net/note/edit/117125 Del: integrated -- https://master.php.net/note/delete/117125/integrated Del: useless -- https://master.php.net/note/delete/117125/useless Del: bad code -- https://master.php.net/note/delete/117125/bad+code Del: spam -- https://master.php.net/note/delete/117125/spam Del: non-english -- https://master.php.net/note/delete/117125/non-english Del: in docs -- https://master.php.net/note/delete/117125/in+docs Del: other reasons-- https://master.php.net/note/delete/117125 Reject -- https://master.php.net/note/reject/117125 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202293) next »