DB
class DB
Support database operations.
Properties
| private int | $_count | Number of items returned. |
|
| private string | $_dbDriver | The database driver. |
|
| private bool | $_error | Error status. |
|
| private int | $_fetchStyle | The fetch style. |
|
| static private DB | $_instance | Instance of this class. |
|
| private int | $_lastInsertID | Id of last item inserted into database. |
|
| private PDO | $_pdo | The PDO object. |
|
| private mixed | $_query | The query. |
|
| private mixed | $_result | The result. |
Methods
This constructor creates a new PDO object as an instance variable. If there are any failures the application quits with an error message.
Begins a database transaction.
Constructs join statements for SQL queries.
Commits the current database transaction.
Establishes a new database connection using the provided configuration array.
Number of rows affected or returned by the last query.
Performs delete operation against SQL database.
Getter function for the $_error variable.
Performs find operation against the database. The user can use parameters such as conditions, bind, order, limit, and sort.
Returns only the first row of the last query.
Returns number of records in a table.
Returns first result in the _result array.
Returns columns for a table.
An instance of this class set as a variable. To be used in other class because we can't use $this.
Appropriately formats column for query with GROUP BY operations. A call to the ANY_VALUE function is added if the DB driver is MySQL or MariaDB.
Perform insert operations against the database.
Checks if a transaction is currently active.
The primary key ID of the last insert operation.
Performs database query operations that includes prepare, binding, execute, and fetch.
Supports SELECT operations that maybe ran against a SQL database. It supports the ability to order and limit the number of results returned from a database query. The user can use parameters such as conditions, bind, order, limit, and sort.
Returns all rows from the last query
Rolls back the current database transaction.
Checks whether a given table exists in the currently connected database.
Performs update operation on a SQL database record.
Updates records in a table using params-style conditions.
Check if a value exists in a JSON or text-based column
Details
at line 76
private
__construct()
This constructor creates a new PDO object as an instance variable. If there are any failures the application quits with an error message.
at line 112
bool
beginTransaction()
Begins a database transaction.
at line 123
protected string
_buildJoin(array $join = [])
Constructs join statements for SQL queries.
at line 137
bool
commit()
Commits the current database transaction.
at line 169
static void
connect(array $override)
Establishes a new database connection using the provided configuration array.
This method creates a new PDO instance based on the given connection details and sets it as the singleton instance for the DB class. It can be used to override the default database connection at runtime (e.g., for testing or connecting to a different database).
Supported drivers:
- sqlite: Connects to a SQLite database file or an in-memory database. Foreign key constraints are enabled by default.
- mysql: Connects to a MySQL/MariaDB database using the provided host, port, database name, charset, username, and password.
at line 198
int
count()
Number of rows affected or returned by the last query.
at line 215
bool
delete(string $table, int $id)
Performs delete operation against SQL database.
Example setup: $contacts = $db->delete('contacts', 3);
at line 225
bool
error()
Getter function for the $_error variable.
at line 251
bool|array
find(string $table, array $params = [], bool|string $class = false)
Performs find operation against the database. The user can use parameters such as conditions, bind, order, limit, and sort.
Example setup: $contacts = $db->find('users', [ 'conditions' => ["email = ?"], 'bind' => ['chad.chapman@email.com'], 'order' => "username", 'limit' => 5, 'sort' => 'DESC' ]);
at line 271
array|object|bool
findFirst(string $table, array $params = [], bool|string $class = false)
Returns only the first row of the last query.
at line 288
int
findTotal(string $table, array $params = [])
Returns number of records in a table.
at line 302
array|object
first()
Returns first result in the _result array.
at line 314
array
getColumns(string $table)
Returns columns for a table.
at line 330
static DB
getInstance()
An instance of this class set as a variable. To be used in other class because we can't use $this.
at line 342
PDO
getPDO()
Returns instance of PDO class.
at line 355
static string|null
groupByColumn(string $column)
Appropriately formats column for query with GROUP BY operations. A call to the ANY_VALUE function is added if the DB driver is MySQL or MariaDB.
at line 385
bool
insert(string $table, array $fields = [])
Perform insert operations against the database.
Example setup: $fields = [ 'fname' => 'John', 'lname' => 'Doe', 'email' => 'example@email.com' ]; $contacts = $db->insert('contacts', $fields);
at line 415
bool
inTransaction()
Checks if a transaction is currently active.
at line 424
int|string|null
lastID()
The primary key ID of the last insert operation.
at line 442
DB
query(string $sql, array $params = [], bool|string $class = false)
Performs database query operations that includes prepare, binding, execute, and fetch.
at line 493
protected bool
_read(string $table, array $params = [], bool|string $class = false, bool $count = false)
Supports SELECT operations that maybe ran against a SQL database. It supports the ability to order and limit the number of results returned from a database query. The user can use parameters such as conditions, bind, order, limit, and sort.
at line 599
array
results()
Returns all rows from the last query
at line 608
bool
rollBack()
Rolls back the current database transaction.
at line 623
bool
tableExists(string $table)
Checks whether a given table exists in the currently connected database.
This method runs a driver-specific query to determine if the table is present.
For SQLite, it queries the sqlite_master system table. For MySQL/MariaDB,
it uses the SHOW TABLES LIKE statement.
at line 652
bool
update(string $table, int $id, array $fields = [])
Performs update operation on a SQL database record.
Example setup: $fields = [ 'fname' => 'John', 'email' => 'example@email.com' ]; $contacts = $db->update('contacts', 3, $fields);
at line 667
int
updateWhere(string $table, array $fields, array $params = [])
Updates records in a table using params-style conditions.
at line 703
bool
valueExistsInColumn(string $table, string $column, mixed $value)
Check if a value exists in a JSON or text-based column