PDO::Query() returning false

后端 未结 1 1387
遇见更好的自我
遇见更好的自我 2021-01-15 04:36

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

1条回答
  •  野的像风
    2021-01-15 05:37

    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]);
    

    0 讨论(0)
提交回复
热议问题