karlpatrickespiritu / simple-php-pdo-wrapper
a wrapper for php's pdo extension
v0.5
2016-05-02 06:13 UTC
Requires
- php: >=5.1.0
Requires (Dev)
- phpunit/phpunit: ^5.1
This package is not auto-updated.
Last update: 2024-12-17 20:43:31 UTC
README
a wrapper for php's pdo extension.
NO LONGER MAINTAINED. (Please use Laravel's Eloquent: https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel )
Installation
via composer
$ composer require karlpatrickespiritu/simple-php-pdo-wrapper
Getting Started
Edit the src/DB.config.php
file for your database configuration.
<?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!
Usage
Namespacing
Make sure that this namespace is declared every time using the singleton DB
class.
use PDO\DB;
Fetching
Fetch a single row in the result set
$singleRow = DB::i()->fetch('SELECT * FROM `users` WHERE `name` = :name', ['name' => 'Justin Beiber']);
Fetch a multiple rows in the result set
$multipleRows = DB::i()->fetchRows('SELECT * FROM `users`');
Extras
$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