[英]PDO Fatal error: Cannot use object of type PDOException as array
I'm attempting to learn some PDO, moving away from sql_connect! 我正尝试学习一些PDO,而不是sql_connect! (not even mysqli).
(甚至不是mysqli)。
I am having this error: Fatal error: Cannot use object of type PDOException as array 我遇到此错误:致命错误:不能将PDOException类型的对象用作数组
Here is the code: 这是代码:
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
}
The exception that gets thrown is: SQLSTATE[HY000]: General error 引发的异常是:SQLSTATE [HY000]:一般错误
$sql = "insert into employee (firstname,lastname,department) VALUES (?,?,?)";
$stm = $conn->prepare($sql);
$stm->execute(array($firstname, $lastname, $dept));
$insertId = $conn->lastInsertId();
Next time try google first. 下次尝试Google。 "PDO auto increment" will give you answer in seconds.
“ PDO自动递增”将在几秒钟内为您提供答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.