国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP實現的數據庫封裝類

2020-03-24 19:07:18
字體:
來源:轉載
供稿:網友
?php
html' target='_blank'>class Core{
/*對數組進行繼承*/
static function inHerit($arr_orgin,$arr_output){
return array_merge($arr_orgin,$arr_output);

} /*打印錯誤*/
static function throwError($errmsg){
echo ' p error:'.$errmsg.' /p
exit();
}
}
?
?php
class db{
private $result = array();
private $connector = array();
private $configs = array();
private $active = 0;
private $default_config = array(
'dbtype' = 'mysql',
'index' = 0,'user' = 'root', 'pwd' = '' ,'host' = 'localhost' ,'port'= 3306,'charset' = 'utf8' ,'dbname' = null
);

/*初始化*/
public function __construct($config = array()){
if($config) $this- connect($config);
} public function __destruct(){
foreach($this- connector as $index = $connect)
$this- {'_'.$this- configs[$index]['dbtype'].'_close'}($connect);

} private function _mysql_close($connect){
mysqli_close($connect);
}
/*選擇連接*/
public function selectConnect($index){
return isset($this- connector[$index]) (($this- active = $index) || true);
}
/*建立連接*/
public function connect($config){
(!isset($config['index'])) $config['index'] = $this- default_config['index']++ ; $config = Core::inHerit($this- default_config,$config);
!in_array($config['dbtype'],array('mysql')) Core::throwError('未支持的數據庫類型');
extract($config);
$this- configs[$index] = $config;
$this- {'_'.$config['dbtype'].'_connect'}($user, $pwd,$host,$dbname,$charset,$index,$port);

}
private function _mysql_connect($user = 'root', $pwd = '' ,$host = 'localhost' ,$dbname = null,$charset = 'utf8' ,$index = 0 ,$port = 3306){
$this- connector[$index] = mysqli_connect ( $host, $user, $pwd , null, $port) or Core::throwError(mysql_error());
$this- active = $index;
if($dbname) $this- selectDb($dbname,$charset);
}

/*取得當前連接*/
private function _getConnect(){
return $this- connector[$this- active];
}
/*取得當前連接使用的數據庫類型 帶參數自動拼接為函數名*/
private function _getDbTypeFunc($funcname = null){
$dbtype = $this- configs[$this- active]['dbtype'];
return $funcname?'_'.$dbtype.'_'.$funcname:$dbtype;
}
/*選擇數據庫*/
public function selectDb($dbname,$charset){
mysqli_select_db($this- _getConnect(),$dbname) or Core::throwError(mysql_error());
$this- _mysql_query('set names '.$charset);
}

/*執行語句*/
private function _mysql_query($sql){
$result = mysqli_query($this- _getConnect(),$sql) or Core::throwError(mysql_error());
return $result;
} private function _mysql_bind_by_name($sql , $sqlv){
$sql_param = array();

$getparam = $getparam2 = '/w+?';// $getparam = array_keys($sqlv); implode('|',$getparam);
preg_match_all('/:('.$getparam.')/b/iU',$sql,$getparam) or Core::throwError('參數綁定錯誤');

$getparam = $getparam[1];
$getparam = array_flip($getparam); count($getparam) != count($sqlv) || array_diff_key($getparam,$sqlv) Core::throwError('參數不匹配'); $sql = preg_replace('/:('.$getparam2.')/b/iU','?',$sql) ;

$sqlv = array_merge($getparam,$sqlv);

unset($getparam2);
unset($getparam);


$stmt = mysqli_prepare($this- _getConnect(),$sql) or Core::throwError('wrong sql:'.$sql);


$ptype = '';
$bindparam = array($stmt,'');

foreach ($sqlv as $k = $v){
$ptype .= $this- _getParamType($k);
$bindparam[] = $v;
}
$bindparam[1] = $ptype;

call_user_func_array('mysqli_stmt_bind_param',$bindparam);
return $this- _mysql_stmt_exec($stmt,$sql);
} private function _mysql_bind_in_sort(){
$argus = func_get_args() ;
$sql = array_shift($argus);
$stmt = mysqli_prepare($this- _getConnect(),$sql) or Core::throwError('wrong sql:'.$sql);
$pcount = mysqli_stmt_param_count($stmt);

$pcount != count($argus) Core::throwError('參數不匹配');
$ptype = str_repeat('s',$pcount);
array_splice($argus, 0, 0 ,array($stmt,$ptype));
call_user_func_array('mysqli_stmt_bind_param',$argus); return $this- _mysql_stmt_exec($stmt,$sql);
} private function _mysql_stmt_exec($stmt,$sql){
mysqli_stmt_execute($stmt); if($this- isSelect($sql)){ ///返回值綁定

mysqli_stmt_bind_result($stmt,$a);
while ($stmt- fetch()) {
print_r($a);
echo ' br/
}
}
$stmt- close();

} /*根據參數形式使用不同函數*/
private function _mysql_iquery($sql , $sqlv = array()){
if ($sqlv){
if (is_array($sqlv))
return $this- _mysql_bind_by_name($sql , $sqlv);
else{
$argus = func_get_args() ;
return call_user_func_array(array($this,'_mysql_bind_in_sort'),$argus);
}
}else{
return $this- _mysql_query($sql,$sqlv);
}
}

/*根據鍵名前綴區分數據類型*/
private function _getParamType($key){
$r = 's';
/*首字母小寫 第二個字母大寫 則第一個字母為模式前綴*/
$mode = array(
'i' = 'i','s' = 's','d' = 'd','b' = 'b'
);
if (strlen($key) = 2 $key[0] == strtolower($key[0]) $key[1] == strtoupper($key[1]) in_array($key[0],$mode))
$r = $mode[$key[0]];

return $r;
}
/*分析sql語句 判斷行為*/
private function _cmdType($sql){
return substr(strtolower($sql),0,strpos($sql,' '));

}
/*分析sql語句是否為select語句*/
private function isSelect($sql){
return 'select' == $this- _cmdType($sql);
}
public function query($sql , $sqlv = array()){
$func = $this- _getDbTypeFunc('iquery');

$argus = func_get_args() ;
$argus[0] = trim($argus[0]);
return call_user_func_array(array($this,$func),$argus);
}

}
? ?php
$d = array(
'dbname' = 'test',
'charset' = 'latin1'
);
$db = new db($d);//數組方式綁定參數
$sqlv = array('iId1'= '14','id2' = 30);
$r = $db - query( select id from toselect where id :iId1 and id :id2 ,$sqlv);//標準方式綁定參數
$r = $db - query( select content from toselect where id ? ,13);var_dump($r);
?html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永宁县| 额尔古纳市| 南京市| 阿巴嘎旗| 工布江达县| 贵德县| 迁西县| 阳新县| 嵊泗县| 精河县| 宣武区| 高青县| 黔东| 鄂尔多斯市| 黑河市| 松滋市| 雷山县| 黄石市| 全南县| 宜君县| 湖口县| 巴里| 石景山区| 鸡泽县| 稷山县| 孟州市| 永修县| 临沭县| 富裕县| 博爱县| 靖远县| 邹城市| 巴中市| 大埔县| 张家界市| 高陵县| 青州市| 扎赉特旗| 九龙县| 青浦区| 渑池县|