抱歉,我是 PDO 新手,对 PHP 很生疏。我有一个 PHP 页面,当 PDO 调用失败时,我试图在其中显示错误消息。这是我所拥有的,但甚至没有失败显示后的 echo 语句。
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$orderNumber = $_GET['orderID'];
echo "Order number: $orderNumber";
// Get all orders from database for this user
$sql = "SELECT userID, order_files.orderID, order_files.serviceID, SUM(file_total) as orderTotal, date_created FROM order_files
LEFT JOIN orders ON order_files.orderID = orders.orderID where order_files.orderID = :orderID GROUP BY order_files.orderID";
try {
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($conn->errorInfo());
}
$stmt->execute(array('orderID' => $orderNumber));
$results = $stmt->fetchAll();
var_dump($results);
} catch (PDOException $pdoError) {
echo "I'm sorry, Dave. I'm afraid I can't do that.";
throw new error($pdoError->getMessage());
}
我错过了什么?我还尝试将 PDO::errorInfo() 块在执行语句之后进一步向下移动,正如我在另一个答案中看到的那样,它是一个潜在的罪魁祸首。没有运气。
页面在 $stmt = $conn->prepare($sql); 之后停止执行所有内容
抱歉,这最终成为一个愚蠢的问题。需要修改 php.ini 以显示错误。 拍额头