From 0e367f797fa9348408881ed758976f21e8c667e4 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Tue, 29 Jul 2025 09:46:46 +0200 Subject: [PATCH] fix: only set `WHERE` filters on `delete()` if conditions are provided (#51) This PR fixes a bug where if no conditions are passed to `MySQL->from("table")->delete()`, the generated query will look like this: ```sql DELETE FROM `table` WHERE ``` This is obviously invalid SQL syntax. This PR only adds the `WHERE` keyword and rules if conditions have been supplied to either `where()` or with `delete([])` Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/51 --- src/MySQL.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/MySQL.php b/src/MySQL.php index 1dff427..a7deac6 100644 --- a/src/MySQL.php +++ b/src/MySQL.php @@ -268,9 +268,14 @@ $this->throw_if_no_table(); // Set DELETE WHERE conditions from arguments - $this->where(...$conditions); + if ($conditions) { + $this->where(...$conditions); + } - $sql = "DELETE FROM `{$this->table}` WHERE {$this->filter_sql}"; + // Get array of SQL WHERE string and filter values + $filter_sql = !is_null($this->filter_sql) ? " WHERE {$this->filter_sql}" : ""; + + $sql = "DELETE FROM `{$this->table}`{$filter_sql}"; return $this->execute_query($sql, self::to_list_array($this->filter_values)); }