simple-php-pdo-wrapper
a wrapper for php's pdo extension., (*1)
NO LONGER MAINTAINED. (Please use Laravel's Eloquent: https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel ), (*2)
Installation
via composer, (*3)
$ composer require karlpatrickespiritu/simple-php-pdo-wrapper
Getting Started
Edit the src/DB.config.php file for your database configuration., (*4)
<?php
return [
/**
* Your database configuration. Modify accordingly.
*
* @see http://php.net/manual/en/pdo.construct.php
*/
'DB_CONFIG' => [
'host' => 'localhost',
'port' => '',
'dbname' => '',
'username' => 'root',
'password' => ''
],
/**
* Default PDO attributes. Modify accordingly.
*
* @see http://php.net/manual/en/pdo.setattribute.php
*/
'PDO_ATTRIBUTES' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
];
And that's it, you're good to go!, (*5)
Usage
Namespacing
Make sure that this namespace is declared every time using the singleton DB class., (*6)
use PDO\DB;
Fetching
Fetch a single row in the result set, (*7)
$singleRow = DB::i()->fetch('SELECT * FROM `users` WHERE `name` = :name', ['name' => 'Justin Beiber']);
Fetch a multiple rows in the result set, (*8)
$multipleRows = DB::i()->fetchRows('SELECT * FROM `users`');
Extras, (*9)
$multipleRows = DB::i()->fetchRows('SELECT * FROM `users`');
$rows = DB::i()->getRowCount(); // get the number of rows returned
$columns = DB::i()->getColumnCount(); // get the number of columns per row
var_dump($multipleRows);
var_dump($rows);
var_dump($columns);
Insertion
$params = [
':address' => 'Urgello St., Cebu City, 6000, Cebu',
':name' => 'Karl Espiritu',
':email' => 'wiwa.espiritu@gmail.com'
];
DB::i()->exec("INSERT INTO `users`(`name`, `address`, `email`) VALUES (:name, :address, :email)", $params);
var_dump(DB::i()->getLastId()); // dumps the last inserted id
Update
$params = [
":name" => "John Mayer",
":email" => "mayer@somesite.com",
":id" => 3
];
$affectedRows = DB::i()->exec("UPDATE `users` SET name = :name, email = :email WHERE id = :id", $params);
var_dump($affectedRows); // dumps the number of affected rows
Deletion
$affectedRows = DB::i()->exec("DELETE FROM `users` WHERE id = :id", [':id' => 1]);
var_dump($affectedRows); // dumps the number of affected rows
Maintainers
License
(C) Karl Patrick Espiritu 2015, released under the MIT license, (*10)