我有一个
PHP脚本,在SQL查询的语句中有两个故意错字错误:
try { $stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE non-existent_column=?"); $stmt->blindValue(1,$id,PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchColumn(); } catch(PDOException $err) { var_dump($err->getMessage()); var_dump($dbh->errorInfo()); }
但是,即使将属性设置为PDO :: ERRMODE_EXCEPTION,脚本也不会捕获错误.我在这里想念的是什么?
更新:
<?PHP $user= "user"; $password = "password"; $dsn = "MysqL:dbname=Catalogue;host=localhost"; $dbh = new PDO($dsn,$user,$password); $dbh->setAttribute(PDO::ERRMODE_EXCEPTION); $id = 1000; try { $stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE non-existent_column=?"); $stmt->bindValue(1,PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchColumn(); } catch(PDOException $err) { echo "caught"; var_dump($err->getMessage()); var_dump($dbh->errorInfo()); exit(); } var_dump($stmt); var_dump($row); echo "uncaught"; exit(); ?>
您对setAttribute()的调用缺少第一个参数:
原文链接:https://www.f2er.com/php/135083.html$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
如果你没有得到一个
Warning: PDO::setAttribute() expects exactly 2 parameters,1 given
您的error_reporting level对于开发服务器来说太低了/和/或者您没有关注the error log或没有设置display_errors=On(您更喜欢它;我更喜欢错误日志而不是display_errors).
编辑:请试试
<?PHP echo 'PHP version: ',PHPversion(),"\n"; try { $dbh = new PDO('MysqL:host=localhost;dbname=test;charset=utf8','localonly','localonly'); echo 'client version: ',$dbh->getAttribute(PDO::ATTR_CLIENT_VERSION),"\n"; echo 'server version: ',$dbh->getAttribute(PDO::ATTR_SERVER_VERSION),"\n"; $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); } catch(PDOException $err) { var_dump($err->getMessage()); die('...'); } $id = 'foo'; try { $stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE `non-existent_column`=?"); $stmt->bindValue(1,PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchColumn(); } catch(PDOException $err) { var_dump($err->getMessage()); var_dump($dbh->errorInfo()); die('....'); } echo 'done.';
印在我的机器上
PHP version: 5.3.5 client version: MysqLnd 5.0.7-dev - 091210 - $Revision: 304625 $ server version: 5.5.8 string(94) "sqlSTATE[42S22]: Column not found: 1054 Unknown column 'non-existent_column' in 'where clause'" array(3) { [0]=> string(5) "42S22" [1]=> int(1054) [2]=> string(54) "Unknown column 'non-existent_column' in 'where clause'" } ....