aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2025-02-18 14:47:53 +0100
committerHarald Eilertsen <haraldei@anduin.net>2025-06-29 12:01:58 +0200
commit5796c198931764ea0eb3557a0e9795cf1b1c70ee (patch)
treea497fdbc2d83a19f86330a2df8db387320e4dc18
parente32584000b24624e8096386fe504b69084781442 (diff)
downloadvolse-hubzilla-improve-mysql-insert.tar.gz
volse-hubzilla-improve-mysql-insert.tar.bz2
volse-hubzilla-improve-mysql-insert.zip
WIP: Make db insert handle tables with weird id'simprove-mysql-insert
-rw-r--r--include/dba/dba_pdo.php31
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;
}
/**