Skip to content

Commit aaf353c

Browse files
Fix/ecakubi enhance delete function ThingEngineer#1017
1 parent a8de11f commit aaf353c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

MysqliDb.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ public function update($tableName, $tableData, $numRows = null)
939939

940940
/**
941941
* Delete query. Call the "where" method first.
942+
* For better error handling, we can make use of exceptions.
943+
* This will allow the caller to handle the error in a more specific way, including providing more specific error messages.
942944
*
943945
* @param string $tableName The name of the database table to work with.
944946
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
@@ -950,7 +952,7 @@ public function update($tableName, $tableData, $numRows = null)
950952
public function delete($tableName, $numRows = null)
951953
{
952954
if ($this->isSubQuery) {
953-
return;
955+
throw new Exception('Delete function cannot be used within a subquery context.');
954956
}
955957

956958
$table = self::$prefix . $tableName;
@@ -962,13 +964,16 @@ public function delete($tableName, $numRows = null)
962964
}
963965

964966
$stmt = $this->_buildQuery($numRows);
965-
$stmt->execute();
967+
// Error handling
968+
if (!$stmt->execute()) {
969+
throw new Exception('Failed to execute delete operation: ' . $this->_stmtError);
970+
}
966971
$this->_stmtError = $stmt->error;
967972
$this->_stmtErrno = $stmt->errno;
968973
$this->count = $stmt->affected_rows;
969974
$this->reset();
970975

971-
return ($stmt->affected_rows > -1); // -1 indicates that the query returned an error
976+
return ($stmt->affected_rows >= 0); // anything greater than -1 indicates success
972977
}
973978

974979
/**

0 commit comments

Comments
 (0)