简体   繁体   中英

PDO Fatal error: Cannot use object of type PDOException as array

I'm attempting to learn some PDO, moving away from sql_connect! (not even mysqli).

I am having this error: Fatal error: Cannot use object of type PDOException as array

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

$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. "PDO auto increment" will give you answer in seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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