Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php  
  2.     class pdo_handler{
  3.    
  4.         public $DBH;
  5.    
  6.         //Connection, MIGHT OUTSOURCE IT TO INDIA (aka another file).
  7.         public function __construct(){
  8.             $host = 'localhost';
  9.             $dbname = 'wedding';
  10.             $user = 'root';
  11.             $pass = 'root';
  12.             try{
  13.             //MySQL with PDO_MYSQL  
  14.                 $this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  15.             }catch(PDOException $e){
  16.                 echo $e->getMessage();
  17.             }
  18.    
  19.         }
  20.        
  21.         //Function to insert data, takes an associative array, builds a strings to insert data securely using PDO in the table specified.
  22.         public function insert($array, $table){
  23.             $string = '';
  24.            
  25.             foreach($array as $key => $value){
  26.                 $string .=  ":" . $key . ", ";
  27.             }
  28.            
  29.             $string =  substr($string, 0, -2);
  30.            
  31.             $STH = $this->DBH->prepare("INSERT INTO $table values(" . $string . ")");
  32.             $STH->execute($array);
  33.         }
  34.        
  35.        
  36.         //Takes an associate array with all the values to be changed, $table is a string containing the table name, and the condition is a string containing any desired condition.
  37.         public function update($array, $table, $condition = ''){
  38.             $string = '';
  39.            
  40.             foreach($array as $key => $value){
  41.                 $string .=  "$key = :" . $key . ", ";
  42.             }
  43.            
  44.             $string =  substr($string, 0, -2);
  45.             $STH = $this->DBH->prepare("UPDATE $table SET $string $condition");
  46.             $STH->execute($array);
  47.            
  48.         }
  49.        
  50.         //Delete
  51.         public function delete($table, $condition = ''){
  52.             $STH = $this->DBH->prepare("DELETE from $table $condition");
  53.             $STH->execute();
  54.         }
  55.        
  56.         //Select function returns an array with all the rows affected, each array has all the column names requested, also returns the [rows] affected
  57.         public function select($what, $where){         
  58.             $i = 0;
  59.             $select = array();
  60.            
  61.             $STH = $this->DBH->query("SELECT $what from $where");
  62.             $STH->setFetchMode(PDO::FETCH_ASSOC);
  63.            
  64.             while($row = $STH->fetch()){
  65.                 foreach($row as $key => $value){
  66.                     $select[$i][$key] = $value;
  67.                 }
  68.                 $i++;
  69.             }
  70.             $data = array(
  71.                 'select' => $select,
  72.                 'rows' => $i
  73.             );
  74.             return $data;
  75.         }
  76.        
  77.         //Accepts a string for simple existance comparison in another table
  78.         public function exists($what, $where){
  79.             $data = $this->select("*", "$where where $what");
  80.             if($data['rows'] == 0){
  81.                 return false;
  82.             }else{
  83.                 return true;
  84.             }
  85.         }
  86.        
  87.     }
  88.    
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement