mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-14 08:43:40 +02:00
feat(doc): add ORDER BY and LIMIT to README
This commit is contained in:
parent
6d4f7fc44b
commit
8b69a17cde
1 changed files with 55 additions and 7 deletions
62
README.md
62
README.md
|
@ -43,8 +43,10 @@ $db->get(
|
||||||
array|string $columns,
|
array|string $columns,
|
||||||
// (Optional) key, value array of column names and values to filter with WHERE <column> = <value>
|
// (Optional) key, value array of column names and values to filter with WHERE <column> = <value>
|
||||||
?array $filter = null,
|
?array $filter = null,
|
||||||
|
// (Optional) order by columns name => direction ("ASC"|"DESC")
|
||||||
|
?array $order_by = null,
|
||||||
// (Optional) max number of rows to return
|
// (Optional) max number of rows to return
|
||||||
?int $limit = null
|
int|array|null $limit = null
|
||||||
): array|bool;
|
): array|bool;
|
||||||
// Returns array of arrays for each row, or bool if $columns = null
|
// Returns array of arrays for each row, or bool if $columns = null
|
||||||
```
|
```
|
||||||
|
@ -93,15 +95,40 @@ $coffee = $db->get("beverages", $columns, $filter);
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
## LIMIT
|
## ORDER BY
|
||||||
|
|
||||||
You can also pass an optional integer as the 4:th argument to `MySQL->get()` and `LIMIT` the rows to match
|
You can also pass an associative array as the 4:th argument to `MySQL->get()` to `ORDER BY` a column or multiple columns
|
||||||
|
|
||||||
> **Note**
|
|
||||||
> Passing (int) `1` will flatten the returned array to two dimensions (k => v)
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$coffee = $db->get("beverages", $columns, $filter, 1);
|
$coffee = $db->get("beverages", $columns, $filter, ["beverage_name" => "ASC"], 2);
|
||||||
|
// SELECT beverage_name, beverage_size FROM beverages ORDER BY beverage_name ASC LIMIT 2
|
||||||
|
```
|
||||||
|
```php
|
||||||
|
[
|
||||||
|
[
|
||||||
|
"beverage_name" => "tea",
|
||||||
|
"beverage_size" => 10
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"beverage_name" => "tea",
|
||||||
|
"beverage_size" => 15
|
||||||
|
],
|
||||||
|
// ...etc for "beverage_name = coffee"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
## LIMIT
|
||||||
|
|
||||||
|
You can also pass an optional integer or associative array as the 5:th argument to `MySQL->get()` and `LIMIT` the rows to match.
|
||||||
|
|
||||||
|
> **Note**
|
||||||
|
> Passing (int) `1` will flatten the returned array from `get()` to two dimensions (k => v)
|
||||||
|
|
||||||
|
### Passing an integer to LIMIT
|
||||||
|
This will simply `LIMIT` the results returned to the integer passed
|
||||||
|
|
||||||
|
```php
|
||||||
|
$coffee = $db->get("beverages", $columns, $filter, null, 1);
|
||||||
// SELECT beverage_name, beverage_size FROM beverages WHERE beverage_type = "coffee" LIMIT 1
|
// SELECT beverage_name, beverage_size FROM beverages WHERE beverage_type = "coffee" LIMIT 1
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
|
@ -111,6 +138,27 @@ $coffee = $db->get("beverages", $columns, $filter, 1);
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Passing an associative array to LIMIT
|
||||||
|
This will `OFFSET` and `LIMIT` the results returned from the first key of the array as `OFFSET` and the value of that key as `LIMIT`
|
||||||
|
|
||||||
|
```php
|
||||||
|
$coffee = $db->get("beverages", $columns, $filter, null, [3 => 2]);
|
||||||
|
// SELECT beverage_name, beverage_size FROM beverages LIMIT 3 OFFSET 2
|
||||||
|
```
|
||||||
|
```php
|
||||||
|
[
|
||||||
|
[
|
||||||
|
"beverage_name" => "tea",
|
||||||
|
"beverage_size" => 10
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"beverage_name" => "tea",
|
||||||
|
"beverage_size" => 15
|
||||||
|
],
|
||||||
|
// ...etc
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
# INSERT
|
# INSERT
|
||||||
|
|
||||||
Use `MySQL->insert()` to append a new row to a database table
|
Use `MySQL->insert()` to append a new row to a database table
|
||||||
|
|
Loading…
Add table
Reference in a new issue