Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO致命错误:无法将PDOException类型的对象用作数组_Php_Mysql_Pdo_Mysqli - Fatal编程技术网

Php PDO致命错误:无法将PDOException类型的对象用作数组

Php PDO致命错误:无法将PDOException类型的对象用作数组,php,mysql,pdo,mysqli,Php,Mysql,Pdo,Mysqli,我正在尝试学习一些PDO,远离sql_connect!连迈斯克利都没有 我有一个错误: 致命错误:无法将PDOException类型的对象用作数组 代码如下: try { $sql = "insert into employee (firstname,lastname,department) VALUES (':firstname',':lastname',':dept')"; $resultSet = $conn->prepare($sql); $data = array('first

我正在尝试学习一些PDO,远离sql_connect!连迈斯克利都没有

我有一个错误: 致命错误:无法将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();

下次先试试谷歌。PDO auto increment将在几秒钟内给出答案。

从插入查询中获取任何内容的想法是从何而来的?我想从刚才插入的内容中获取ID employeeid=auto inc,以便稍后在代码中使用。我希望得到我刚才插入的结果。