繁体   English   中英

PDO致命错误:不能将PDOException类型的对象用作数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM