katiek

MySQL Connection / Prepared Statement Manager

Dec 21st, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. class Connector
  2. {
  3.     public $ok;
  4.     public $db;
  5.     public $prepared;
  6.     public function __construct(){
  7.     // db should be a reasonable mysqli instance.
  8.         $this->db = new mysqli("host","usename","password","default schema",port,"");
  9.         $this->prepared = array();
  10.     }
  11.  
  12.  
  13.     public function prepare($name, $sql, $types, $fetch="none", $firstonly=false){     // fetch:none/one/assoc/flat/onecol/id
  14.         $this->prepared[$name] = array($this->db->prepare($sql),$types,$fetch,$firstonly,$sql);
  15.     }
  16.     public function execute($name,$data=array()){
  17.         if(!is_array($data)){
  18.             $data = array($data);
  19.         }
  20.         $this->ok=true;
  21.         $stmt = $this->prepared[$name][0];
  22.         $type = $this->prepared[$name][1];
  23.         $ftch = $this->prepared[$name][2];
  24.         $frst = $this->prepared[$name][3];
  25.         if (!is_object($stmt)) {
  26.             $this->ok=false;
  27.             return -1;
  28.         }
  29.         call_user_func_array('mysqli_stmt_bind_param', array_merge (array($stmt, $type), $data));
  30.         $stmt->execute();
  31.         if($stmt->error!=""){
  32.             $this->ok=false;
  33.             return -1;
  34.         }
  35.         if($ftch=="one" || $ftch=="assoc" || $ftch=="flat" || $ftch=="onecol"){
  36.             $meta = $stmt->result_metadata();
  37.             while ($field = $meta->fetch_field()) {
  38.                 $params[] = &$row[$field->name];
  39.             }
  40.             call_user_func_array(array($stmt, 'bind_result'), $params);
  41.             $empty=true;
  42.             while ($stmt->fetch()) {
  43.                 $c=array();
  44.                 foreach($row as $key => $val) {
  45.                     $empty=false;
  46.                     if($ftch=="assoc"){
  47.                         $c[$key] = $val;
  48.                     }else{
  49.                         $c[] = $val;
  50.                     }
  51.                     if($ftch=="one"){
  52.                         $stmt->free_result();
  53.                         return $val;
  54.                     }
  55.                 }
  56.                 if($frst)
  57.                     return $c;
  58.                 if($ftch=="onecol")
  59.                     $hits[] = $c[0];
  60.                 else
  61.                     $hits[] = $c;
  62.             }
  63.             if($ftch=="id"){
  64.                 return $this->db->insert_id;
  65.                 }
  66.             if($empty)
  67.                 return null;
  68.             return $hits;
  69.             $stmt->free_result();
  70.         }
  71.         return $this->db->insert_id;
  72.     }
  73.     public function describe(){
  74.         $out = "";
  75.         foreach($this->prepared as $key => $i){
  76.             $out.= $key." = ".$i[4]." (".$i[1].") ";
  77.             if($i[3]){
  78.                 if($i[2]=="flat"){
  79.                     $out.="flat value list for first row";
  80.                 }else if($i[2]=="assoc"){
  81.                     $out.="assoc key-value pairs for first row";
  82.                 }
  83.             }else{
  84.                 if($i[2]=="flat"){
  85.                     $out.="lflat value list for each row";
  86.                 }else if($i[2]=="assoc"){
  87.                     $out.="assoc key-value pairs for each row";
  88.                 }else if($i[2]=="onecol"){
  89.                     $out.="a single value for each row";
  90.                 }else{
  91.                     $out.="no output";
  92.                 }
  93.             }
  94.             $out.="\n";
  95.         }
  96.         return $out;
  97.     }
  98.    
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment