我有以下代码片段:
error_reporting(E_ALL);
$hostname = "localhost";
$username = "user";
$password = "password";
try {
$db = new PDO("mysql:host=$hostname;dbname=DBNAME", $username, $password);
echo "connected...<br/>\n";flush();
$sql = "SELECT COLA, COLB FROM TABLEA LIMIT 10";
echo "query:$sql--<br/>\n";flush();
$stmt = $dbh->query($sql);
echo "statement:<pre>";print_r($stmt);echo "</pre>\nfetching...<br/>\n";flush();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result === true) {
echo "result is true<br/>\n";flush();
}
else if ($result === false) {
echo "result is false<br/>\n";flush();
}
else {
echo "what?<br/>\n";flush();
}
foreach ($result as $key=>$val) {
echo "key:$key - val:$val--<br/>\n";flush();
}
echo "done";
$dbh = null;
}
catch (Exception $e) {
die ($e->getMessage());
}
正如您所看到的,我有一堆调试回声语句,但我看到的只有:
connected...
query:SELECT COLA, COLB FROM TABLEA LIMIT 10
我期待看到我的陈述对象和我的结果一样显示,我错过了什么?
答案 0 :(得分:3)
初始化PDO对象后,请尝试将error mode设置得更高。
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
默认值为PDO::ERRMODE_SILENT
,不会输出任何警告/错误。使用此默认设置,您必须轮询errorInfo()
以查看错误详细信息。