Crud
extends Database
in package
Database access for a table: prepare and run a query, then fetch its rows.
Table of Contents
Methods
- dsn() : string
- Build a MySQL DSN, for the app's connection and for the CLI scripts that open their own.
- db() : PDO
- The shared database connection, opened on first use.
- read() : array<int, array<string, mixed>>
- Prepare and run a read query, then return its rows.
- process() : array<int, array<string, mixed>>
- Return all rows of an executed statement as associative arrays.
Methods
dsn()
Build a MySQL DSN, for the app's connection and for the CLI scripts that open their own.
public
static dsn(string $host, int $port, string $name) : string
Parameters
- $host : string
-
Host name or IP address, or the path of a Unix socket (it starts with
/). - $port : int
-
TCP port, left out of the DSN when it is MySQL's default (3306) and ignored for a socket.
- $name : string
-
Database name.
Return values
string —The PDO DSN, with the utf8mb4 charset.
db()
The shared database connection, opened on first use.
protected
final static db() : PDO
Building, validating or formatting a model therefore needs no database. The connection comes from the DB_*
constants; a failure triggers E_USER_ERROR. With DEBUG on, statements are created as DebugStatement so every
query execution is timed.
Prepared statements are native, so integer columns come back as int; the charset is utf8mb4 and the timezone
is the server's.
Return values
PDO —The shared connection.
read()
Prepare and run a read query, then return its rows.
protected
final read(string $sql[, array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false $param = false ]) : array<int, array<string, mixed>>
Parameters
- $sql : string
-
SQL query with bind placeholders.
- $param : array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false = false
-
Bind parameters (each: [name, value, PDO type, length]), or false for none.
Return values
array<int, array<string, mixed>> —The result rows.
process()
Return all rows of an executed statement as associative arrays.
private
process(PDOStatement $query) : array<int, array<string, mixed>>
Parameters
- $query : PDOStatement
-
Executed statement.
Return values
array<int, array<string, mixed>> —The result rows.