PHP PDO Wrapper Class
A minimal extension for PHP's PDO class designed to make running SQL statements easier., (*1)
Download the latest release, (*2)
Project Overview
This project provides a minimal extension for PHP's PDO (PHP Data Objects) class designed for ease-of-use and saving development time/effort. This is achived by providing methods - delete, insert, select, and update - for quickly building common SQL statements, handling exceptions when SQL errors are produced, and automatically returning results/number of affected rows for the appropriate SQL statement types., (*3)
System Requirements
-
PHP 5.5+ (any current PHP version, see https://secure.php.net/supported-versions.php), (*4)
-
PDO Extension, (*5)
-
Appropriate PDO Driver(s) - PDO_SQLITE, PDO_MYSQL, PDO_PGSQL, (*6)
-
Only MySQL, SQLite, and PostgreSQL database types are currently supported., (*7)
db Class Methods
Below you will find a detailed explanation along with code samples for each of the 6 methods included in the db class., (*8)
constructor
More information can be found on how to set the dsn parameter by following the links provided below., (*9)
delete
delete("mytable", "Age $lname
)
$db->delete("mytable", "LName = :lname", $bind);
?>
If no SQL errors are produced, this method will return the number of rows affected by the DELETE statement., (*13)
insert
"John",
"LName" => "Doe",
"Age" => 26,
"Gender" => "male"
);
$db->insert("mytable", $insert);
?>
If no SQL errors are produced, this method will return the number of rows affected by the INSERT statement., (*14)
See also: $db->lastInsertId(); which is inherited from PDO., (*15)
run
run($sql);
//SQLite
$sql = run($sql);
?>
This method is used to run free-form SQL statements that can't be handled by the included delete, insert, select, or update methods. If no SQL errors are produced, this method will return the number of affected rows for DELETE, INSERT, and UPDATE statements, or an associate array of results for SELECT, DESCRIBE, and PRAGMA statements., (*16)
select
select("mytable");
//SELECT #2
$results = $db->select("mytable", "Gender = 'male'");
//SELECT #3 w/Prepared Statement
$search = "J";
$bind = array(
":search" => "%$search"
);
$results = $db->select("mytable", "FName LIKE :search", $bind);
?>
setErrorCallbackFunction
setErrorCallbackFunction("myErrorHandler");
/*
Text Version
$db->setErrorCallbackFunction("myErrorHandler", "text");
Internal/Built-In PHP Function
$db->setErrorCallbackFunction("echo");
*/
$results = $db->select("mynonexistingtable");
?>
When a SQL error occurs, this project will send a formatted (html or text) error message to a callback function specified through the setErrorCallbackFunction method. The callback function's name should be supplied as a string without parenthesis. As you can see in the examples provided above, you can specify an internal/built-in PHP function or a custom function you've created., (*17)
If no SQL errors are produced, this method will return an associative array of results., (*18)
update
"Jane",
"Gender" => "female"
);
$db->update("mytable", $update, "FName = 'John'");
//Update #2 w/Prepared Statement
$update = array(
"Age" => 24
);
$fname = "Jane";
$lname = "Doe";
$bind = array(
":fname" => $fname,
":lname" => $lname
);
$db->update("mytable", $update, "FName = :fname AND LName = :lname", $bind);
?>
If no SQL errors are produced, this method will return the number of rows affected by the UPDATE statement., (*19)