我试图学习一些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]:常规错误
答案 0 :(得分:0)
$sql = "insert into employee (firstname,lastname,department) VALUES (?,?,?)";
$stm = $conn->prepare($sql);
$stm->execute(array($firstname, $lastname, $dept));
$insertId = $conn->lastInsertId();
下次先试试谷歌。 " PDO自动增量"会在几秒钟内给你回答。