diff options
-rw-r--r-- | include/dba/dba_pdo.php | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 3b0edefcd..eccd35ba9 100644 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -166,9 +166,31 @@ class dba_pdo extends dba_driver { // The last inserted id is kept for each connection, so we're not risking // a race condition wrt inserts by other requests that happen simultaneously. // - $id = $this->db->lastInsertId($table); + $id = intval($this->db->lastInsertId($table)); + + if ($id === 0) { + // The last insert failed, or the primary key of the table is + // not an auto_increment column. + $sqlstate = $res->errorCode(); + + if (intval($sqlstate) === 0) { + // No error reported, so use the passed in id if it exists. + if (isset($data[$idcol])) { + $id = $data[$idcol]; + } else { + var_dump(dbq('SELECT LAST_INSERT_ID()')); + db_logger("Unable to retreive inserted row, no {$idcol} was specified."); + return false; + } + } else { + db_logger('Insert failed, error info: ' . var_export($res->errorInfo(), true)); + return false; + } + } - $res = $this->q("SELECT * FROM {$table} WHERE {$idcol} = {$id}"); + if ($id !== 0) { + $res = $this->q("SELECT * FROM {$table} WHERE {$idcol} = {$id}"); + } if (is_a($res, PDOStatement::class)) { db_logger('dba_pdo: PDOStatement returned, did not expect that.'); @@ -180,9 +202,10 @@ class dba_pdo extends dba_driver { // Since we should never have more than one result, unwrap the array // so we only have the resulting row. $res = $res[0]; + return $res; + } else { + return false; } - - return $res; } /** |