I\'m attempting to learn and use PDO in PHP. I\'ve come across an issue in the query() method.
I\'m attempting to use $sth = $db->query(\"SELECT * FROM titl
Use PDO's errorinfo() function to find out why.
if( ! $sth = $db->query("SELECT * FROM titles ORDER BY RAND() LIMIT 1") ) {
die(var_export($db->errorinfo(), TRUE));
}
Late Update
In the interest of making my old answers better, setting PDO to throw exceptions on error is far more manageable than checking every function return.
$dbh = new PDO($connstr, $user, $pwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Or, more concisely:
$dbh = new PDO($connstr, $user, $pwd, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);