PHP Classes

PHP PDO DB Connection Controller: Connect to a database configured using a subclass

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 200 All time: 8,470 This week: 92Up
Version License PHP version Categories
db-controller 1.0.0GNU General Publi...5PHP 5, Databases
Description 

Author

This class can connect to a database configured using a subclass.

It can connect to a given database supported by the PHP PDO extension using classes variables that an application can define using a sub-class of this class.

The class can also perform other database access actions forwarding calls to the PDO object, for instance:

- Preparing queries

- Setting query parameter values

- Executing queries

- Retrieve query results

Innovation Award
PHP Programming Innovation award nominee
March 2022
Number 8
PDO is a popular PHP extension that provides several interfaces for classes that can access many types of SQL databases.

Usually, the developer defines a connection to a specific database type using a connection string with a particular syntax.

Many developers prefer to set connection parameters using specific values passing individual parameters because it is more clear to configure each parameter.

This class provides a means to connect to a database using PDO by specifying the connection parameter values using class variables of a sub-class of this class.

Manuel Lemos
Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 25 packages by
Country: Nigeria Nigeria
Age: 32
All time rank: 204910 in Nigeria Nigeria
Week rank: 9 Up2 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 11x

Recommendations

What is the best PHP pdo mysql class?
PHP Project using PDO MySQL Class

Documentation

db-controller

Php PDO wrapper

Installation

Installation is super-easy via Composer:

composer require peterujah/db-controller

USAGES

Initialize DBController with configuration array

$handler = new \Peterujah\NanoBlock\DBController($configArray);

Or extend \Peterujah\NanoBlock\DBController to set your connection details like below

class Conn extends \Peterujah\NanoBlock\DBController{ 
	public function __construct(bool $development = false){
		$config = array(
			"PORT" => 3306,
			"HOST" => "localhost",
			"VERSION" => "mysql",
		);
		if($development){
			$config["USERNAME"] = "root";
			$config["PASSWORD"] = "";
			$config["NAME"] = "dbname";
		}else{
			$config["USERNAME"] = "dbusername";
			$config["PASSWORD"] = "dbpass";
			$config["NAME"] = "dbname";
		} 
		$this->onDebug = $development;
		$this->config = $config;
		$this->onCreate();
	}
}

Initialize your custom class

$handler = new Conn($_SERVER["HOST_NAME"]=="localhost");

Now run query select, insert, update, delete etc.. using prepare statment

$handler->prepare('SELECT * FROM users WHERE username = :username LIMIT 1');
$handler->bind(':username', "Peter");
$handler->execute();
$res = $handler->getOne();
$handler->free();

Or run query select, insert, update, delete etc.. using query

$handler->query('SELECT * FROM users');
$res = $handler->getAll();
$handler->free();

| Options | Description | |-----------------|-------------------------------------------------------------------------------------| | prepare(string) | Call "prepare" with sql query string to prepare query execution | | query(string) | Call "query" width sql query without "bind" and "param" | | bind(param, value, type) | Call "bind" to bind value to the pdo prepare method | | param(param, value, type) | Call "param" to bind parameter to the pdo statment | | execute() | Execute prepare statment | | rowCount() | Get result row count | | getOne() | Get one resault row, this is useful when you set LIMIT 1 | | getAll() | Retrieve all result | | getInt() | Gets integer useful when you select COUNT() | | getAllObject() | Gets result object | | getLastInsertedId() | Gets list inserted id from table | | free() | Free database connection | | dumpDebug() | Dump debug sql query parameter | | errorInfo() | Print PDO prepare statment error when debug is enabled | | error() | Print connection or execution error when debug is enabled | | setDebug(bool) | Sets debug status | | setConfig(array) | Sets connection config array | | conn() | Retrieve DBController Instance useful when you call "setConfig(config)" |

Connection Config array example

[
     PORT => 3306,
     HOST => "localhost",
     VERSION => "mysql",
     NAME => "dbname",
     USERNAME => "root",
     PASSWORD => ""
]

			

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file DBController.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file Config.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:200
This week:0
All time:8,470
This week:92Up