trollolodn icon

#php #mysqli_connect #json

trollolodn | PRO | 02/01/13 08:14:27 AM UTC | 0 ⭐ | 167 👁️ | Never ⏰ | []
PHP |

2.36 KB

|

None

|

0 👍

/

0 👎

/*@Author: quocvuongdn
*@Twitter: quocvuongdn
*@Site: quocvuongdn.com
*/
 
class DBmysqli{
    protected $db_name = '';
    protected $db_user = '';
    protected $db_pass = '';
    protected $db_host = '';
    protected $db;
 
    function __construct(){
        $this->connect();
 
    }
 
    public function connect(){
        $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
        if(!$this->db){
            echo mysqli_connect_error();
        }
        $this->db->set_charset("utf8");
    }
 
    public function executeNonParams($prcName){
            $result = $this->db->query('call '.$prcName.'()');
            if($result->num_rows == 1)
                return $this->processRowSet($result, true);
            return $this->processRowSet($result);
    }
 
    public function executeParams($prcName, $arrParams){
            $sql = 'call '.$prcName.' (';
            for($i= 0;$i< count($arrParams);$i++){
                    if($i== count($arrParams)-1){
                        if(is_string($arrParams[$i])){
                            $sql .= '"'.$arrParams[$i].'");';
                        }else{
                            $sql .= $arrParams[$i].')';
                        }
                    }else{
                        if(is_string($arrParams[$i])){
                            $sql .= '"'.$arrParams[$i].'", ';
                        }else{
                            $sql .= $arrParams[$i].', ';
                        }
                    }
            }
            $result = $this->db->query($sql);
            if($result->num_rows == 1){
                return $this->processRowSet($result, true);
            }
            return $this->processRowSet($result);
    }
 
    public function processRowSet($result, $singleRow = false){
            $resultArray = array();
            while ($row = $result->fetch_assoc()) {
                    array_push($resultArray, $row);
            }
            $result->close();
            $this->db->next_result();
            if($singleRow == true)
                return $this->json($resultArray[0]);
            return $this->json($resultArray);
    }
 
    public function json($resultArray){
            if(is_array($resultArray))
                    return json_encode($resultArray);
            return $resultArray;
    }
 
    public function close(){
        $this->db->close();
    }
}

Comments