trollolodn

#php #mysqli_connect #json

Feb 1st, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. /*@Author: quocvuongdn
  2. *@Twitter: quocvuongdn
  3. *@Site: quocvuongdn.com
  4. */
  5.  
  6. class DBmysqli{
  7.     protected $db_name = '';
  8.     protected $db_user = '';
  9.     protected $db_pass = '';
  10.     protected $db_host = '';
  11.     protected $db;
  12.  
  13.     function __construct(){
  14.         $this->connect();
  15.  
  16.     }
  17.  
  18.     public function connect(){
  19.         $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
  20.         if(!$this->db){
  21.             echo mysqli_connect_error();
  22.         }
  23.         $this->db->set_charset("utf8");
  24.     }
  25.  
  26.     public function executeNonParams($prcName){
  27.             $result = $this->db->query('call '.$prcName.'()');
  28.             if($result->num_rows == 1)
  29.                 return $this->processRowSet($result, true);
  30.             return $this->processRowSet($result);
  31.     }
  32.  
  33.     public function executeParams($prcName, $arrParams){
  34.             $sql = 'call '.$prcName.' (';
  35.             for($i= 0;$i< count($arrParams);$i++){
  36.                     if($i== count($arrParams)-1){
  37.                         if(is_string($arrParams[$i])){
  38.                             $sql .= '"'.$arrParams[$i].'");';
  39.                         }else{
  40.                             $sql .= $arrParams[$i].')';
  41.                         }
  42.                     }else{
  43.                         if(is_string($arrParams[$i])){
  44.                             $sql .= '"'.$arrParams[$i].'", ';
  45.                         }else{
  46.                             $sql .= $arrParams[$i].', ';
  47.                         }
  48.                     }
  49.             }
  50.             $result = $this->db->query($sql);
  51.             if($result->num_rows == 1){
  52.                 return $this->processRowSet($result, true);
  53.             }
  54.             return $this->processRowSet($result);
  55.     }
  56.  
  57.     public function processRowSet($result, $singleRow = false){
  58.             $resultArray = array();
  59.             while ($row = $result->fetch_assoc()) {
  60.                     array_push($resultArray, $row);
  61.             }
  62.             $result->close();
  63.             $this->db->next_result();
  64.             if($singleRow == true)
  65.                 return $this->json($resultArray[0]);
  66.             return $this->json($resultArray);
  67.     }
  68.  
  69.     public function json($resultArray){
  70.             if(is_array($resultArray))
  71.                     return json_encode($resultArray);
  72.             return $resultArray;
  73.     }
  74.  
  75.     public function close(){
  76.         $this->db->close();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment