Class Database{
private $host='192.168.5.206';
private $user='root';
private $pass='bts@@@';
private $name='TMKT_test';
private $db;
function __construct(){
$this->connect();
}
function connect(){
$this->db = new mysqli($this->host, $this->user, $this->pass, $this->name);
if(!$this->db){
echo mysqli_connect_error();
}
$this->db->set_charset('utf8');
}
function executeParams($prcName, $arrParams = array(), $isFunc = false, $vReturn = 'return'){
if(!$this->connect()){
$this->connect();
}
$sql = "call $prcName(";
$lastStr = "";
if($isFunc == true){
$sql = "select $prcName(";
}
if(count($arrParams) > 0){
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].', ';
}
}
}
} else{
$lastStr = ')';
}
if($isFunc == true){
$lastStr .= ' as '.$vReturn.';';
}
//echo '<pre>';
//var_dump($sql.$lastStr);
$result = $this->db->query($sql.$lastStr);
//var_dump($result);
if (!$result) {
throw new Exception("Database Error [{$this->db->errno}] {$this->db->error}");
} else{
if($result->num_rows == 1){
return $this->fetchData($result, true);
}
return $this->fetchData($result);
}
}
function executeSql($script = ''){
$sql = $script;
//var_dump($script);
$result = $this->db->query($sql);
if (!$result) {
throw new Exception("Database Error [{$this->db->errno}] {$this->db->error}");
} else{
if($result->num_rows == 1){
return $this->fetchData($result, true);
}
return $this->fetchData($result);
}
}
function fetchData($result, $singleRow = false){
$returnArray = array();
for ($i=0; $i < $result->num_rows; $i++) {
array_push($returnArray, $result->fetch_assoc());
}
$result->close();
$result->next_result;
//if($singleRow == true){
// return $returnArray[0];
//}
return $returnArray;
}
}
Comments