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.
Getter function for the private _count variable.
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 the first result performed by an SQL query. It is a wrapper for the _read function for this purpose.
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 value of query results.
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()
Getter function for the private _count variable.
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 272
array|object|bool
findFirst(string $table, array $params = [], bool|string $class = false)
Returns the first result performed by an SQL query. It is a wrapper for the _read function for this purpose.
at line 289
int
findTotal(string $table, array $params = [])
Returns number of records in a table.
at line 303
array|object
first()
Returns first result in the _result array.
at line 315
array
getColumns(string $table)
Returns columns for a table.
at line 331
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 343
PDO
getPDO()
Returns instance of PDO class.
at line 356
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 386
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 416
bool
inTransaction()
Checks if a transaction is currently active.
at line 425
int|string|null
lastID()
The primary key ID of the last insert operation.
at line 443
DB
query(string $sql, array $params = [], bool|string $class = false)
Performs database query operations that includes prepare, binding, execute, and fetch.
at line 494
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 600
array
results()
Returns value of query results.
at line 609
bool
rollBack()
Rolls back the current database transaction.
at line 624
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 653
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 668
int
updateWhere(string $table, array $fields, array $params = [])
Updates records in a table using params-style conditions.
at line 704
bool
valueExistsInColumn(string $table, string $column, mixed $value)
Check if a value exists in a JSON or text-based column