Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* add your database login details to db::connect() */
- /* sample usage */
- /*
- $sql = "drop table if exists db_test";
- db::query($sql);
- echo "dropping table db_test\n";
- $sql = "create table db_test(id int auto_increment primary key, name varchar(100), number int)";
- db::query($sql);
- echo "<hr>creating table db_test\n";
- $sql = "insert into db_test(name, number) values (:name, :number)";
- $arr = array(":name" => "Hello", ":number" => 44);
- $id = db::query($sql, $arr);
- echo "<hr>sample insert, id: $id\n";
- $sql = "insert into db_test(name, number) values (:name, :number)";
- $arr = array(":name" => "World", ":number" => 55);
- $id = db::query($sql, $arr);
- echo "<hr>second insert, id: $id\n";
- $sql = "update db_test set number = :number";
- $arr = array(":number" => 77);
- $rows_affected = db::query($sql, $arr);
- echo "<hr>sample update, $rows_affected rows affected\n";
- $sql = "select * from db_test where name like :name";
- $arr = array(":name" => "%");
- $result = db::query($sql, $arr);
- echo "<hr>sample select returns an array of rows,<br>\n";
- if(count($result) == 0)
- echo "no records found<br>.\n";
- foreach($result as $row){
- echo htmlentities($row['id'] . ' ' . $row['name'] . ' ' . $row['number']) . "<br>\n";
- }
- $sql = "select * from db_test where id = :id";
- $arr = array(":id" => 1);
- $row = db::query_row($sql, $arr);
- echo "<hr>sample select using query_row() to return a single row array,<br>\n";
- if(!$row)
- echo "row not found\n";
- else
- echo "row found: " . htmlentities($row['id'] . ' ' . $row['name'] . ' ' . $row['number']) . "\n";
- $sql = "select name from db_test where id = :id";
- $arr = array(":id" => 1);
- $val = db::query_val($sql, $arr);
- echo "<hr>sample select using query_val() to return a single value,<br>\n";
- if(!$val)
- echo "value not found\n";
- else
- echo "value found: " . htmlentities($val) . "\n";
- db::connect();
- db::$dbh->beginTransaction(); // speed up multi inserts with a transaction
- for($i = 0; $i < 1000; $i++){
- $sql = "insert into db_test(name, number) values (:name, :number)";
- $arr = array(":name" => "Name $i", ":number" => $i);
- db::query($sql, $arr);
- }
- db::$dbh->commit(); // commit transaction
- echo "<hr>inserted $i records in a transaction\n";
- $sql = "delete from db_test where number = :number or 1=1";
- $arr = array(":number" => 66);
- $rows_affected = db::query($sql, $arr);
- echo "<hr>sample delete, $rows_affected rows affected\n";
- */
- class db{
- // purpose: simple pdo wrapper, only tested on mysql
- // configure settings in connect()
- private static $db_connection_string = null;
- private static $db_user = null;
- private static $db_pass = null;
- private static $display_error_detail = null;
- public static $dbh = null;
- public static function connect(){
- if(self::$dbh)
- return;
- // *** edit this block according to how you detect your enviroment ***
- if(getenv('ENV') == 'development'){
- self::$db_connection_string = 'mysql:host=localhost;dbname=test;';
- self::$db_user = 'root';
- self::$db_pass = '';
- self::$display_error_detail = true; // show full error in development
- }
- else{
- self::$db_connection_string = 'mysql:host=localhost;dbname=test;';
- self::$db_user = 'root';
- self::$db_pass = '';
- self::$display_error_detail = false; // hide errors in production, only "query error" is displayed
- }
- try{
- self::$dbh = new PDO(self::$db_connection_string, self::$db_user, self::$db_pass);
- }
- catch(PDOException $e){
- die('pdo connection error: ' . $e->getMessage());
- }
- }
- public static function query($sql, $sql_param = array()){
- // purpose: run a query
- // returns:
- // array of rows for selects
- // false on errors
- // id for insert statements
- // # rows affected for updates and deletes
- self::connect();
- $sth = self::$dbh->prepare($sql);
- // handle errors
- if(!$sth->execute($sql_param)){
- $arr = $sth->errorInfo();
- $error = $arr[2];
- if(self::$display_error_detail)
- echo "<pre style='color: red;'>" . htmlentities("Error: $error\n\n$sql\n\n") . "Parameters:\n" . print_r($sql_param, true) . "</pre>";
- else
- echo "<pre style='color: red;'>query error</pre>";
- return false;
- }
- // return insert id for insert and rows affected for update, delete, and replace
- if(preg_match('/^\s*(insert|replace)/i', $sql))
- return self::$dbh->lastInsertId();
- elseif(preg_match('/^\s*(update|delete)/i', $sql))
- return $sth->rowCount();
- if($sth->columnCount() == 0)
- return array();
- return $sth->fetchAll(PDO::FETCH_ASSOC);
- }
- public static function query_row($sql, $sql_param = array()){
- // purpose: wrapper for query()
- // returns:
- // a single row array
- // returns null when no results are found
- // returns false on errors
- $result = self::query($sql, $sql_param);
- if($result === false)
- return false;
- elseif(is_array($result) && count($result) > 0)
- return current($result);
- else
- return null;
- }
- public static function query_val($sql, $sql_param = array()){
- // purpose: wrapper for query()
- // returns:
- // a single value
- // returns null when no results are found
- // returns false on errors
- $result = self::query($sql, $sql_param);
- if($result === false)
- return false;
- elseif(is_array($result) && count($result) > 0)
- return current(current($result));
- else
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement