PHP实现的数据库封装类_PHP技巧_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

PHP实现的数据库封装类

作者:黑客防线网安PHP教程基地 来源:黑客防线网安PHP教程基地 浏览次数:0

本篇关键词:数据库实现'&
黑客防线网安网讯:  PHP学习实例,数据库封装类。    <?php    classCore{    /*对数组进行继承*/    staticfunctioninHerit($arr_orgin,$arr_output){    returnarray_merge($arr_org...
  PHP学习实例数据库封装类
  
  <?php
  
  classCore{
  
  /*对数组进行继承*/
  
  staticfunctioninHerit($arr_orgin,$arr_output){
  
  returnarray_merge($arr_orgin,$arr_output);
  
  }
  
  /*打印错误*/
  
  staticfunctionthrowError($errmsg){
  
  echo'&lt;p>error:'.$errmsg.'</p>';
  
  exit();
  
  }
  
  }
  
  ?>
  
  <?php
  
  classdb{
  
  private$result=array();
  
  private$connector=array();
  
  private$configs=array();
  
  private$active=0;
  
  private$default_config=array(
  
  'dbtype'=>'mysql',
  
  'index'=&gt;0,'user'=&gt;'root','pwd'=&gt;'','host'=&gt;'localhost','port'=&gt;3306,'charset'=&gt;'utf8','dbname'=&gt;null
  
  );
  
  /*初始化*/
  
  publicfunction__construct($config=array()){
  
  if($config)$this-&gt;connect($config);
  
  }
  
  publicfunction__destruct(){
  
  foreach($this-&gt;connectoras$index=&gt;$connect)
  
  $this-&gt;{'_'.$this-&gt;configs[$index]['dbtype'].'_close'}($connect);
  
  }
  
  privatefunction_mysql_close($connect){
  
  mysqli_close($connect);
  
  }
  
  /*选择连接*/
  
  publicfunctionselectConnect($index){
  
  returnisset($this-&gt;connector[$index])&&(($this-&gt;active=$index)||true);
  
  }
  
  /*建立连接*/
  
  publicfunctionconnect($config){
  
  (!isset($config['index']))&&$config['index']=$this-&gt;default_config['index']++;
  
  $config=Core::inHerit($this-&gt;default_config,$config);
  
  !in_array($config['dbtype'],array('mysql'))&&Core::throwError('未支持的数据库类型');
  
  extract($config);
  
  $this-&gt;configs[$index]=$config;
  
  $this-&gt;{'_'.$config['dbtype'].'_connect'}($user,$pwd,$host,$dbname,$charset,$index,$port);
  
  }
  
  privatefunction_mysql_connect($user='root',$pwd='',$host='localhost',$dbname=null,$charset='utf8',$index=0,$port=3306){
  
  $this-&gt;connector[$index]=mysqli_connect($host,$user,$pwd,null,$port)orCore::throwError(mysql_error());
  
  $this-&gt;active=$index;
  
  if($dbname)$this-&gt;selectDb($dbname,$charset);
  
  }
  
  /*取得当前连接*/
  
  privatefunction_getConnect(){
  
  return$this-&gt;connector[$this-&gt;active];
  
  }
  
  /*取得当前连接使用的数据库类型带参数自动拼接为函数名*/
  
  privatefunction_getDbTypeFunc($funcname=null){
  
  $dbtype=$this-&gt;configs[$this-&gt;active]['dbtype'];
  
  return$funcname?'_'.$dbtype.'_'.$funcname:$dbtype;
  
  }
  
  /*选择数据库*/
  
  publicfunctionselectDb($dbname,$charset){
  
  mysqli_select_db($this-&gt;_getConnect(),$dbname)orCore::throwError(mysql_error());
  
  $this-&gt;_mysql_query('setnames'.$charset);
  
  }
  
  /*执行语句*/
  
  privatefunction_mysql_query($sql){
  
  $result=mysqli_query($this-&gt;_getConnect(),$sql)orCore::throwError(mysql_error());
  
  return$result;
  
  }
  
  privatefunction_mysql_bind_by_name($sql,$sqlv){
  
  $sql_param=array();
  
  $getparam=$getparam2='w+?';//$getparam=array_keys($sqlv);implode('|',$getparam);
  
  preg_match_all('/:('.$getparam.')/iU',$sql,$getparam)orCore::throwError('参数绑定错误');
  
  $getparam=$getparam[1];
  
  $getparam=array_flip($getparam);
  
  count($getparam)!=count($sqlv)||array_diff_key($getparam,$sqlv)&&Core::throwError('参数不匹配');
  
  $sql=preg_replace('/:('.$getparam2.')/iU','?',$sql);
  
  $sqlv=array_merge($getparam,$sqlv);
  
  unset($getparam2);
  
  unset($getparam);
  
  $stmt=mysqli_prepare($this-&gt;_getConnect(),$sql)orCore::throwError('wrongsql:'.$sql);
  
  $ptype='';
  
  $bindparam=array($stmt,'');
  
  foreach($sqlvas$k=&gt;$v){
  
  $ptype.=$this-&gt;_getParamType($k);
  
  $bindparam[]=$v;
  
  }
  
  $bindparam[1]=$ptype;
  
  call_user_func_array('mysqli_stmt_bind_param',$bindparam);
  
  return$this-&gt;_mysql_stmt_exec($stmt,$sql);
  
  }
  
  privatefunction_mysql_bind_in_sort(){
  
  $argus=func_get_args();
  
  $sql=array_shift($argus);
  
  $stmt=mysqli_prepare($this-&gt;_getConnect(),$sql)orCore::throwError('wrongsql:'.$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-&gt;_mysql_stmt_exec($stmt,$sql);
  
  }
  
  privatefunction_mysql_stmt_exec($stmt,$sql){
  
  mysqli_stmt_execute($stmt);
  
  if($this-&gt;isSelect($sql)){///返回值绑定
  
  mysqli_stmt_bind_result($stmt,$a);
  
  while($stmt-&gt;fetch()){
  
  print_r($a);
  
  echo'<br/>';
  
  }
  
  }
  
  $stmt-&gt;close();
  
  }
  
  /*根据参数形式使用不同函数*/
  
  privatefunction_mysql_iquery($sql,$sqlv=array()){
  
  if($sqlv){
  
  if(is_array($sqlv))
  
  return$this-&gt;_mysql_bind_by_name($sql,$sqlv);
  
  else{
  
  $argus=func_get_args();
  
  returncall_user_func_array(array($this,'_mysql_bind_in_sort'),$argus);
  
  }
  
  }else{
  
  return$this-&gt;_mysql_query($sql,$sqlv);
  
  }
  
  }
  
  /*根据键名前缀区分数据类型*/
  
  privatefunction_getParamType($key){
  
  $r='s';
  
  /*首字母小写第二个字母大写则第一个字母为模式前缀*/
  
  $mode=array(
  
  'i'=&gt;'i','s'=&gt;'s','d'=&gt;'d','b'=&gt;'b'
  
  );
  
  if(strlen($key)&gt;=2&&$key[0]==strtolower($key[0])&&$key[1]==strtoupper($key[1])&&in_array($key[0],$mode))
  
  $r=$mode[$key[0]];
  
  return$r;
  
  }
  
  /*分析sql语句判断行为*/
  
  privatefunction_cmdType($sql){
  
  returnsubstr(strtolower($sql),0,strpos($sql,''));
  
  }
  
  /*分析sql语句是否为select语句*/
  
  privatefunctionisSelect($sql){
  
  return'select'==$this-&gt;_cmdType($sql);
  
  }
  
  publicfunctionquery($sql,$sqlv=array()){
  
  $func=$this-&gt;_getDbTypeFunc('iquery');
  
  $argus=func_get_args();
  
  $argus[0]=trim($argus[0]);
  
  returncall_user_func_array(array($this,$func),$argus);
  
  }
  
  }
  
  ?>
  
  <?php
  
  $d=array(
  
  'dbname'=>'test',
  
  'charset'=&gt;'latin1'
  
  );
  
  $db=newdb($d);
  
  //数组方式绑定参数
  
  $sqlv=array('iId1'=&gt;'14','id2'=&gt;30);
  
  $r=$db-&gt;query("selectidfromtoselectwhereid&gt;:iId1andid<:id2",$sqlv);
  
  //标准方式绑定参数
  
  $r=$db->query("selectcontentfromtoselectwhereid&gt;?",13);
  
  var_dump($r);
  
  ?>
  
  
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-17534-1.html
网站维护教程更新时间:2012-09-21 05:20:40  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer