Advertisement
Guest User

PHP mysqli

a guest
Feb 13th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('VALID_ACCESS')) {
  4.     echo -8;
  5.     die('You don\'t belong here!');
  6. }
  7.  
  8. function insertRows($rows, $len) {
  9.     $query = prepareQuery($rows, $len);
  10.     $con = new db('localhost', 'root', 'shibboleet', 'test');
  11.     if (mysqli_connect_errno()) {
  12.         echo -4;
  13.         die('Couldn\'t connect to database! ' . mysqli_connect_error());
  14.     }
  15.     else {
  16.         if (!($stmt = $con->prepare($query))) {
  17.             echo -5;
  18.             die('Couldn\'t connect to database! ' . $con->error);
  19.         }
  20.         bindParams($rows, $len, $stmt);
  21.        
  22.         if ($stmt->execute()) {
  23.             echo 0;
  24.         }
  25.         else {
  26.             echo -9;
  27.             die('Error: ' . $stmt->error . '<br/>');
  28.         }
  29.         $stmt->close();
  30.     }
  31.    
  32.     $con->close();
  33. }
  34.  
  35. function prepareQuery($rows, $len) {
  36.     global $base, $sepr, $typeMap, $colMap;
  37.     $str = $base . '(' . implode($sepr, $colMap) . ') VALUES ';
  38.     $entries = 0;
  39.     for ($i = 0; $i < $len; $i++) {
  40.         $toAppend = true;
  41.         $query = '(';
  42.        
  43.         $tycnt = count($typeMap) - 1;
  44.         $ncols = substr_count($rows[$i], $sepr);
  45.         if ($ncols == $tycnt) {
  46.             for ($j = 0; $j < $ncols; $j++) {
  47.                 $query .= '?,';
  48.             }
  49.             $query .= '?';
  50.         }
  51.         else {
  52.             $toAppend = false;
  53.         }
  54.        
  55.         $query .= ')';
  56.         if ($i < ($len-1)) {
  57.             $query .= ',';
  58.         }
  59.        
  60.         if ($toAppend) {
  61.             $str .= $query;
  62.             $entries++;
  63.         }
  64.     }
  65.     $str .= ';';
  66.    
  67.     if ($entries == 0) {
  68.         echo -7;
  69.         die('No valid entries provided!');
  70.     }
  71.    
  72.     echo $str . '<br/>';
  73.     return $str;
  74. }
  75.  
  76. function bindParams($rows, $len, $stmt) {
  77.     global $sepr, $typeMap;
  78.     for ($i = 0; $i < $len; $i++) {
  79.         $tycnt = count($typeMap);
  80.         $cols = explode($sepr, $rows[$i]);
  81.        
  82.         if (count($cols) == $tycnt) {
  83.             for ($j =0; $j < $tycnt; $j++) {
  84.                 $prm = base64_decode($cols[$j]);
  85.                 if ($prm == '') {
  86.                     $prm = NULL;
  87.                 }
  88.                 $cols[$j] = $prm;
  89.                 $stmt->mbind_param($typeMap[$j], $cols[$j]);
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. class db extends mysqli {
  96.     public function prepare($query) {
  97.         return new stmt($this,$query);
  98.     }
  99. }
  100.  
  101. class stmt extends mysqli_stmt {
  102.     public function __construct($link, $query) {
  103.         $this->mbind_reset();
  104.         parent::__construct($link, $query);
  105.     }
  106.  
  107.     public function mbind_reset() {
  108.         unset($this->mbind_params);
  109.         unset($this->mbind_types);
  110.         $this->mbind_params = array();
  111.         $this->mbind_types = array('');
  112.     }
  113.  
  114.     public function mbind_param($type, &$param) {
  115.         $this->mbind_types[0].= $type;
  116.         $this->mbind_params[] = &$param;
  117.     }
  118.  
  119.     public function mbind_param_do() {
  120.         $params = array_merge($this->mbind_types, $this->mbind_params);
  121.         return  call_user_func_array(array($this, 'bind_param'),
  122.                 $this->makeValuesReferenced($params));
  123.     }
  124.  
  125.     private function makeValuesReferenced($arr){
  126.         $refs = array();
  127.         foreach($arr as $key => $value) {
  128.             $refs[$key] = &$arr[$key];
  129.         }
  130.         return $refs;
  131.     }
  132.  
  133.     public function execute() {
  134.         if(count($this->mbind_params)) {
  135.             if (!$this->mbind_param_do()) {
  136.                 echo -11;
  137.                 die('Error binding parameters! ' . $this->error . '<br/>');
  138.             }
  139.         }
  140.         return parent::execute();
  141.     }
  142.  
  143.     private $mbind_types = array('');
  144.     private $mbind_params = array();
  145. }
  146.  
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement