Download the PHP package chsxf/pdo-database-manager without Composer
On this page you can find all versions of the php package chsxf/pdo-database-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chsxf/pdo-database-manager
More information about chsxf/pdo-database-manager
Files in chsxf/pdo-database-manager
Package pdo-database-manager
Short Description Database manager based on PDO with extension methods
License MIT
Homepage https://github.com/chsxf/pdo-database-manager
Informations about the package pdo-database-manager
About This Project
The main purpose of this package is to extend the capabilities of PDO by prodiving useful query methods to allow redaction of faster, safer and more reliable code.
Legacy Repository
I originally wrote this package while working on several tools and websites for my now defunct videogame studio called Cheese Burgames. As a fork, this repository is a continuation of the project.
Conventions
This repository uses gitmoji for its commit messages.
Getting Started
Requirements
- PHP 8.0.7+
Installation
We strongly recommend using Composer to install this package.
Usage
The following uses a MySQL database but the same applies for all database servers supported by PDO. Adaptations may be needed though.
Initialization
First, we have to load and initialize the database manager. PDO uses a Data Source Name, or DSN, to convey connection information.
General Queries
You can use the query method to execute any query you like.
In the context of this example, we create a temporary table to proceed with the rest of the helper functions.
Both query and exec methods have been overridden to support error logging, among other things.
As most methods of the DatabaseManager class, query returns false in case of an error, so you can act accordingly.
Executing Altering Statements with Placeholders
Use the exec method in order to retrieve the number of affected rows by an alterning statement like UPDATE or DELETE.
In complement of error logging, this method has been overridden to allow execution of statements with placeholders and values in one call.
Retrieving a Single Specific Value
Use the getValue method to retrieve a single value from your database.
As for the exec method, you can pass a statement with placeholders along with the replacement values in a single call, improving security and productivity.
Retrieval of a Specific Column
The getColumn method fetches the values of the first column returned by the provided statement.
Outputs:
Retrieval of a Specific Row
The getRow method fetches the first row returned by the provided statement.
Along with the get and getIndexed methods, you can specify how rows are fetched.
At the moment, only PDO::FETCH_ASSOC, PDO::FETCH_NUM, PDO::FETCH_OBJ and PDO::FETCH_BOTH (PHP's default) are supported.
Outputs:
Retrieval of Several Rows
The get method fetches all rows returned by the previded statement.
Outputs:
Retrieval of Rows Indexed by a Field Value
The getIndexed method fetches all rows returned by the provided statement, indexed by the value of a specific field.
Outputs:
Retrieval of Pairs of Values
The getPairs method fetches pairs of values returned by the provided statement. The values of the first and second returned columns are used respectively as keys and values of the returned array.
Outputs:
Error Logging
The DatabaseManager class provides an optional error logging system that can help a lot with debugging.
The errors are logged directly into the database in a specific table. It is obviously NOT RECOMMENDED to use this error logging system in production environments.
To enable error logging, pass true to the $useDatabaseErrorLogging parameter in the constructor.
Error Table Structure
In order to work, your database must include a table with the following structure:
By default, the name of the table is database_errors, but you can change it by defining a constant named chsxf\PDO\ERRORS_TABLE before loading the DatabaseManager class.
License
This project is released under the terms of the MIT License.