Compare commits

...

2 commits

Author SHA1 Message Date
73297feb82 fix: use isset() when checking table property value (#52)
We can't access the `$this->table` property before initialization, so let's use `isset()` to check if the property has a value.

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/52
2025-08-30 09:49:14 +02:00
0e367f797f 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
2025-07-29 09:46:46 +02:00

View file

@ -35,7 +35,7 @@
*/
private function throw_if_no_table() {
if (!$this->table) {
if (!isset($this->table)) {
throw new Exception("No table name defined");
}
}
@ -268,9 +268,14 @@
$this->throw_if_no_table();
// Set DELETE WHERE conditions from arguments
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));
}