[英]PDO Fatal error: Cannot use object of type PDOException as array
我正嘗試學習一些PDO,而不是sql_connect! (甚至不是mysqli)。
我遇到此錯誤:致命錯誤:不能將PDOException類型的對象用作數組
這是代碼:
try {
$sql = "insert into employee (firstname,lastname,department) VALUES (':firstname',':lastname',':dept')";
$resultSet = $conn->prepare($sql);
$data = array('firstname' => $firstname, 'lastname' => $lastname, 'dept' => $dept);
$resultSet->execute($data);
$insertCount = $resultSet->rowCount();
//Rows for audit
$auditKey = array();
if ($insertCount == 1){
//This is where it seems to fail on the fetchAll then throws an exception
while ($row = $resultSet->fetchAll(PDO::FETCH_ASSOC)) {
$auditKey[] = $row;
}
}
foreach ($array as $key=> $row) {
$id = $row['employeeid'];
}
} catch (PDOException $e){
//Do something
}
引發的異常是:SQLSTATE [HY000]:一般錯誤
$sql = "insert into employee (firstname,lastname,department) VALUES (?,?,?)";
$stm = $conn->prepare($sql);
$stm->execute(array($firstname, $lastname, $dept));
$insertId = $conn->lastInsertId();
下次嘗試Google。 “ PDO自動遞增”將在幾秒鍾內為您提供答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.