Download the PHP package ezrarieben/pdo-wrapper-singleton without Composer
On this page you can find all versions of the php package ezrarieben/pdo-wrapper-singleton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ezrarieben/pdo-wrapper-singleton
More information about ezrarieben/pdo-wrapper-singleton
Files in ezrarieben/pdo-wrapper-singleton
Package pdo-wrapper-singleton
Short Description A wrapper class written for PDO MySQL DB connections following the singleton pattern
License MIT
Homepage https://github.com/ezrarieben/pdo-wrapper-singleton
Informations about the package pdo-wrapper-singleton
pdo-wrapper-singleton
A wrapper class written in PHP for PDO MySQL DB connections following the singleton pattern.
Table of contents
- pdo-wrapper-singleton
- Table of contents
- Installation
- Basic example
- Usage
- Importing wrapper class
- Setting up DB connection
- Minimal setup example
- Extensive setup example
- Available parameters
- DB interaction
- Using PDO functions
- Prepared statements
- Example
- Example with named parameters
- Error handling
Installation
To use the wrapper in your project, add it as a dependency via composer:
Basic example
Usage
Importing wrapper class
For ease of use it is recommended to import the wrapper class with use
Setting up DB connection
In order to use the wrapper class Database
the connection parameters need to be set first.
There are certain required parameters that need to be set in order for the PDO connection to work.
(See "Required" column in available parameters table for required parameters).
Minimal setup example
Extensive setup example
Available parameters
Description | Setter function | Parameters | Required |
---|---|---|---|
DB server host | setHost() |
string $host |
YES |
User | setUser() |
string $user |
YES |
Password | setPassword() |
string $password |
YES |
DB server host | setPort() |
int $port |
|
Database name | setDbName() |
?string $dbName |
|
PDO attributes | setPdoAttributes() |
array $attributes |
DB interaction
Using PDO functions
All PDO functions can be accessed statically through the Database
class:
Prepared statements
The Database
class has a shortcut function for prepared statements called run()
:
Parameters | Description | Required |
---|---|---|
string $query |
SQL query to execute | YES |
array $params |
parameters to pass to query |
The function returns a PDOStatement
object if preperation and execution of query was successfull.
If preperation or execution of query failed the function will throw a PDOException
or return false
depending on the currently set PDO error mode.
(see: Error handling for more info)
Example
Example with named parameters
Error handling
PDO's error mode is set to ERRMODE_EXCEPTION
by default.
Error handling can therefore be done through try and catch blocks.
When switching to a different error mode you will need to handle errors through booleans.
NOTE: Error handling using booleans is only supported if you change PDO's error mode.