trollolodn

#php #mysqli stored/function (optimize) (not in/out params)

Feb 6th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. Class Database{
  2.     private $host='192.168.5.206';
  3.     private $user='root';
  4.     private $pass='bts@@@';
  5.     private $name='TMKT_test';
  6.     private $db;
  7.  
  8.     function __construct(){
  9.         $this->connect();
  10.     }
  11.  
  12.     function connect(){
  13.         $this->db = new mysqli($this->host, $this->user, $this->pass, $this->name);
  14.         if(!$this->db){
  15.             echo mysqli_connect_error();
  16.         }
  17.         $this->db->set_charset('utf8');
  18.     }
  19.  
  20.     function executeParams($prcName, $arrParams = array(), $isFunc = false, $vReturn = 'return'){
  21.         if(!$this->connect()){
  22.             $this->connect();
  23.         }
  24.         $sql = "call $prcName(";
  25.         $lastStr = "";
  26.         if($isFunc == true){
  27.             $sql = "select $prcName(";
  28.         }
  29.         if(count($arrParams) > 0){
  30.             for($i=0; $i<count($arrParams); $i++){
  31.                 if($i== count($arrParams)-1){
  32.                     if(is_string($arrParams[$i])){
  33.                         $sql .= '"'.$arrParams[$i].'")';
  34.                     }else{
  35.                         $sql .= $arrParams[$i].')';
  36.                     }
  37.                 }else{
  38.                     if(is_string($arrParams[$i])){
  39.                         $sql .= '"'.$arrParams[$i].'", ';
  40.                     }else{
  41.                         $sql .= $arrParams[$i].', ';
  42.                     }
  43.                 }
  44.             }
  45.         } else{
  46.             $lastStr = ')';
  47.         }
  48.         if($isFunc == true){
  49.             $lastStr .= ' as '.$vReturn.';';
  50.         }
  51.         //echo '<pre>';
  52.         //var_dump($sql.$lastStr);
  53.         $result = $this->db->query($sql.$lastStr);
  54.         //var_dump($result);
  55.         if (!$result) {
  56.             throw new Exception("Database Error [{$this->db->errno}] {$this->db->error}");
  57.         } else{
  58.             if($result->num_rows == 1){
  59.                 return $this->fetchData($result, true);
  60.             }
  61.             return $this->fetchData($result);
  62.         }
  63.     }
  64.  
  65.     function executeSql($script = ''){
  66.         $sql = $script;
  67.         //var_dump($script);
  68.         $result = $this->db->query($sql);
  69.         if (!$result) {
  70.             throw new Exception("Database Error [{$this->db->errno}] {$this->db->error}");
  71.         } else{
  72.             if($result->num_rows == 1){
  73.                 return $this->fetchData($result, true);
  74.             }
  75.             return $this->fetchData($result);
  76.         }
  77.     }
  78.  
  79.     function fetchData($result, $singleRow = false){
  80.         $returnArray = array();
  81.         for ($i=0; $i < $result->num_rows; $i++) {
  82.             array_push($returnArray, $result->fetch_assoc());
  83.         }
  84.         $result->close();
  85.         $result->next_result;
  86.         //if($singleRow == true){
  87.         //  return $returnArray[0];
  88.         //}
  89.         return $returnArray;
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment