mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-14 00:33:41 +02:00
Compare commits
3 commits
c64eb96049
...
03868ae784
Author | SHA1 | Date | |
---|---|---|---|
03868ae784 | |||
00cb7b3297 | |||
86f8f2ee76 |
2 changed files with 43 additions and 35 deletions
68
README.md
68
README.md
|
@ -4,19 +4,19 @@ This is a simple abstraction library for MySQL DML operations.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
```php
|
```php
|
||||||
MySQL->for(string $table)
|
MySQL->from(string $table)
|
||||||
->where(?array ...$conditions)
|
->where(?array ...$conditions)
|
||||||
->order(?array $order_by)
|
->order(?array $order_by)
|
||||||
->limit(int|array|null $limit)
|
->limit(?int $limit = null, ?int $offset = null)
|
||||||
->select(array $columns): array|bool;
|
->select(string|array|null $columns = null): mysqli_result|bool;
|
||||||
```
|
```
|
||||||
which would be equivalent to the following in MySQL:
|
which would be equivalent to the following in MySQL:
|
||||||
```sql
|
```sql
|
||||||
SELECT $columns FROM $table WHERE $filter ORDER BY $order_by LIMIT $limit;
|
SELECT `columns` FROM `table` WHERE `filter` ORDER BY `order_by` LIMIT `limit`;
|
||||||
```
|
```
|
||||||
|
|
||||||
> [!IMPORTANT]
|
- All methods can be chained in any order (even multiple times) after a [`for()`](#for) as long as a [`select()`](#select), [`insert()`](#insert), [`update()`](#update), or [`delete()`](#delete) is the last method.
|
||||||
> This library requires the [`MySQL Improved`](https://www.php.net/manual/en/book.mysqli.php) extension and requires PHP 8.0 or newer.
|
- Chaining the same method more than once will override its previous value. Passing `null` to any method that accepts it will unset its value completely.
|
||||||
|
|
||||||
## Install from composer
|
## Install from composer
|
||||||
|
|
||||||
|
@ -28,6 +28,9 @@ composer require vlw/mysql
|
||||||
use vlw\MySQL\MySQL;
|
use vlw\MySQL\MySQL;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> This library requires the [`MySQL Improved`](https://www.php.net/manual/en/book.mysqli.php) extension and PHP 8.0 or newer.
|
||||||
|
|
||||||
# Example / Documentation
|
# Example / Documentation
|
||||||
|
|
||||||
Available statements
|
Available statements
|
||||||
|
@ -60,30 +63,30 @@ $db = new MySQL($host, $user, $pass, $db);
|
||||||
|
|
||||||
All executor methods [`select()`](#select), [`update()`](#update), and [`insert()`](#insert) will return a [`mysqli_result`](https://www.php.net/manual/en/class.mysqli-result.php) object or boolean.
|
All executor methods [`select()`](#select), [`update()`](#update), and [`insert()`](#insert) will return a [`mysqli_result`](https://www.php.net/manual/en/class.mysqli-result.php) object or boolean.
|
||||||
|
|
||||||
# FOR
|
# FROM
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->for(
|
MySQL->from(
|
||||||
string $table
|
string $table
|
||||||
): self;
|
): self;
|
||||||
```
|
```
|
||||||
|
|
||||||
All queries start by chaining the `for(string $table)` method. This will define which database table the current query should be executed on.
|
All queries start by chaining the `from(string $table)` method. This will define which database table the current query should be executed on.
|
||||||
|
|
||||||
*Example:*
|
*Example:*
|
||||||
```php
|
```php
|
||||||
MySQL->for("beverages")->select("beverage_type");
|
MySQL->from("beverages")->select("beverage_type");
|
||||||
```
|
```
|
||||||
|
|
||||||
# SELECT
|
# SELECT
|
||||||
|
|
||||||
Chain `MySQL->select()` anywhere after a [`MySQL->for()`](#for) to retrieve columns from a database table.
|
Chain `MySQL->select()` anywhere after a [`MySQL->from()`](#from) to retrieve columns from a database table.
|
||||||
|
|
||||||
Pass an associative array of strings, CSV string, or null to this method to filter columns.
|
Pass an associative array of strings, CSV string, or null to this method to filter columns.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->select(
|
MySQL->select(
|
||||||
array|string|null $columns
|
string|array|null $columns
|
||||||
): mysqli_result|bool;
|
): mysqli_result|bool;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -91,7 +94,7 @@ In most cases you probably want to select with a constraint. Chain the [`where()
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
```php
|
```php
|
||||||
$beverages = MySQL->for("beverages")->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages
|
$`beverages` = MySQL->from("beverages")->select(["beverage_name", "beverage_size"]); // SELECT `beverage_name`, `beverage_size` FROM beverages
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
[
|
[
|
||||||
|
@ -109,7 +112,7 @@ $beverages = MySQL->for("beverages")->select(["beverage_name", "beverage_size"])
|
||||||
|
|
||||||
# INSERT
|
# INSERT
|
||||||
|
|
||||||
Chain `MySQL->insert()` anywhere after a [`MySQL->for()`](#for) to append a new row to a database table.
|
Chain `MySQL->insert()` anywhere after a [`MySQL->from()`](#from) to append a new row to a database table.
|
||||||
|
|
||||||
Passing a sequential array to `insert()` will assume that you wish to insert data for all defined columns in the table. Pass an associative array of `[column_name => value]` to INSERT data for specific columns (assuming the other columns have a [DEFAULT](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html) value defined).
|
Passing a sequential array to `insert()` will assume that you wish to insert data for all defined columns in the table. Pass an associative array of `[column_name => value]` to INSERT data for specific columns (assuming the other columns have a [DEFAULT](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html) value defined).
|
||||||
|
|
||||||
|
@ -117,20 +120,20 @@ Passing a sequential array to `insert()` will assume that you wish to insert dat
|
||||||
MySQL->insert(
|
MySQL->insert(
|
||||||
// Array of values to INSERT
|
// Array of values to INSERT
|
||||||
array $values
|
array $values
|
||||||
): mysqli_result|bool
|
): bool
|
||||||
// Returns true if row was inserted
|
// Returns true if row was inserted
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->for("beverages")->insert([
|
MySQL->from("beverages")->insert([
|
||||||
null,
|
null,
|
||||||
"coffee",
|
"coffee",
|
||||||
"latte",
|
"latte",
|
||||||
10
|
10
|
||||||
]);
|
]);
|
||||||
// INSERT INTO beverages VALUES (null, "coffee", "latte", 10);
|
// INSERT INTO `beverages` VALUES (null, "coffee", "latte", 10);
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
true
|
true
|
||||||
|
@ -138,12 +141,12 @@ true
|
||||||
|
|
||||||
# DELETE
|
# DELETE
|
||||||
|
|
||||||
Chain `MySQL->delete()` anywhere after a [`MySQL->for()`](#for) to remove a row or rows from the a database table.
|
Chain `MySQL->delete()` anywhere after a [`MySQL->from()`](#from) to remove a row or rows from the a database table.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->delete(
|
MySQL->delete(
|
||||||
array ...$conditions
|
array ...$conditions
|
||||||
): mysqli_result|bool
|
): bool
|
||||||
// Returns true if at least one row was deleted
|
// Returns true if at least one row was deleted
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -152,13 +155,10 @@ This method takes at least one [`MySQL->where()`](#where)-syntaxed argument to d
|
||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->for("beverages")->insert([
|
MySQL->from("beverages")->delete([
|
||||||
null,
|
"beverage_name" => "coffee",
|
||||||
"coffee",
|
|
||||||
"latte",
|
|
||||||
10
|
|
||||||
]);
|
]);
|
||||||
// INSERT INTO beverages VALUES (null, "coffee", "latte", 10);
|
// DELETE FROM `beverages` WHERE `beverage_name` = "coffee";
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
true
|
true
|
||||||
|
@ -166,7 +166,7 @@ true
|
||||||
|
|
||||||
# UPDATE
|
# UPDATE
|
||||||
|
|
||||||
Chain `MySQL->update()` anywhere after a [`MySQL->for()`](#for) to modify existing rows in a database table.
|
Chain `MySQL->update()` anywhere after a [`MySQL->from()`](#from) to modify existing rows in a database table.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
MySQL->update(
|
MySQL->update(
|
||||||
|
@ -178,7 +178,7 @@ MySQL->update(
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
```php
|
```php
|
||||||
MySQL->for("beverages")->update(["beverage_size" => 10]); // UPDATE beverages SET beverage_size = 10
|
MySQL->from("beverages")->update(["beverage_size" => 10]); // UPDATE `beverages` SET `beverage_size` = 10
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
true
|
true
|
||||||
|
@ -201,7 +201,7 @@ MySQL->where(
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
```php
|
```php
|
||||||
$coffee = MySQL->for("beverages")->where(["beverage_type" => "coffee"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages WHERE (beverage_type = "coffee");
|
$coffee = MySQL->from("beverages")->where(["beverage_type" => "coffee"])->select(["beverage_name", "beverage_size"]); // SELECT `beverage_name`, `beverage_size` FROM `beverages` WHERE (`beverage_type` = "coffee");
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
|
@ -229,7 +229,7 @@ MySQL->where([
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
```sql
|
```sql
|
||||||
WHERE (beverage_type = 'coffee' AND beverage_size = 15)
|
WHERE (`beverage_type` = 'coffee' AND `beverage_size` = 15)
|
||||||
```
|
```
|
||||||
|
|
||||||
### OR
|
### OR
|
||||||
|
@ -250,7 +250,7 @@ $filter2 = [
|
||||||
MySQL->where($filter1, $filter2, ...);
|
MySQL->where($filter1, $filter2, ...);
|
||||||
```
|
```
|
||||||
```sql
|
```sql
|
||||||
WHERE (beverage_type = 'coffee' AND beverage_size = 15) OR (beverage_type = 'tea' AND beverage_name = 'black')
|
WHERE (`beverage_type` = 'coffee' AND `beverage_size` = 15) OR (`beverage_type` = 'tea' AND `beverage_name` = 'black')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Define custom operators
|
## Define custom operators
|
||||||
|
@ -284,7 +284,7 @@ MySQL->order(
|
||||||
```
|
```
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$coffee = MySQL->for("beverages")->order(["beverage_name" => "ASC"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages ORDER BY beverage_name ASC
|
$coffee = MySQL->from("beverages")->order(["beverage_name" => "ASC"])->select(["beverage_name", "beverage_size"]); // SELECT `beverage_name`, `beverage_size` FROM `beverages` ORDER BY `beverage_name` ASC
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
|
@ -296,7 +296,7 @@ $coffee = MySQL->for("beverages")->order(["beverage_name" => "ASC"])->select(["b
|
||||||
"beverage_name" => "tea",
|
"beverage_name" => "tea",
|
||||||
"beverage_size" => 15
|
"beverage_size" => 15
|
||||||
],
|
],
|
||||||
// ...etc for "beverage_name = coffee"
|
// ...etc for "`beverage_name` = coffee"
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ MySQL->limit(
|
||||||
This will simply `LIMIT` the results returned to the integer passed
|
This will simply `LIMIT` the results returned to the integer passed
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$coffee = MySQL->for("beverages")->limit(1)->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages WHERE beverage_type = "coffee" LIMIT 1
|
$coffee = MySQL->from("beverages")->limit(1)->select(["beverage_name", "beverage_size"]); // SELECT `beverage_name`, `beverage_size` FROM `beverages` WHERE `beverage_type` = "coffee" LIMIT 1
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
|
@ -330,7 +330,7 @@ $coffee = MySQL->for("beverages")->limit(1)->select(["beverage_name", "beverage_
|
||||||
This will `OFFSET` and `LIMIT` the results returned. The first argument will be the `LIMIT` and the second argument will be its `OFFSET`.
|
This will `OFFSET` and `LIMIT` the results returned. The first argument will be the `LIMIT` and the second argument will be its `OFFSET`.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$coffee = MySQL->for("beverages")->limit(3, 2)->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages LIMIT 3 OFFSET 2
|
$coffee = MySQL->from("beverages")->limit(3, 2)->select(["beverage_name", "beverage_size"]); // SELECT `beverage_name`, `beverage_size` FROM `beverages` LIMIT 3 OFFSET 2
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
|
|
|
@ -66,7 +66,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Use the following table name
|
// Use the following table name
|
||||||
public function for(string $table): self {
|
public function from(string $table): self {
|
||||||
// Reset all definers when a new query begins
|
// Reset all definers when a new query begins
|
||||||
$this->where();
|
$this->where();
|
||||||
$this->limit();
|
$this->limit();
|
||||||
|
@ -76,6 +76,14 @@
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[\Deprecated(
|
||||||
|
message: "use MySQL->from() instead",
|
||||||
|
since: "3.5.7",
|
||||||
|
)]
|
||||||
|
public function for(string $table): self {
|
||||||
|
$this->from($table);
|
||||||
|
}
|
||||||
|
|
||||||
// Create a WHERE statement from filters
|
// Create a WHERE statement from filters
|
||||||
public function where(?array ...$conditions): self {
|
public function where(?array ...$conditions): self {
|
||||||
// Unset filters if null was passed
|
// Unset filters if null was passed
|
||||||
|
|
Loading…
Add table
Reference in a new issue