当前位置:首页 > PHP教程 > php应用 > 列表

PHP以mysqli方式连接类完整代码实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-21 13:22:40 浏览: 评论:0 

这篇文章主要介绍了PHP以mysqli方式连接类完整代码实例,对于学习和了解mysqli都有很大的帮助,需要的朋友可以参考下

本文所述的是一个在PHP中以mysqli方式连接数据库的一个数据库类实例,该数据库类是从一个PHP的CMS中整理出来的,可实现PHP连接数据库类,MySQLi版,兼容PHP4,对于有针对性需要的朋友可根据此代码进行优化和修改。

  1. <?php 
  2. #================================================================================================== 
  3. # Filename: /db/db_mysqli.php 
  4. # Note : 连接数据库类,MySQLi版 
  5. #================================================================================================== 
  6. #[类库sql] 
  7. class db_mysqli 
  8. { 
  9.  var $query_count = 0; 
  10.  var $host; 
  11.  var $user; 
  12.  var $pass; 
  13.  var $data; 
  14.  var $conn; 
  15.  var $result; 
  16.  var $prefix = "qinggan_"; 
  17.  //返回结果集类型,默认是数字+字符 
  18.  var $rs_type = MYSQLI_ASSOC; 
  19.  var $query_times = 0;#[查询时间] 
  20.  var $conn_times = 0;#[连接数据库时间] 
  21.  var $unbuffered = false; 
  22.  //定义查询列表 
  23.  var $querylist; 
  24.  var $debug = false; 
  25.  #[构造函数] 
  26.  function __construct($config=array()) 
  27.  { 
  28.  $this->host = $config['host'] ? $config['host'] : 'localhost'; 
  29.  $this->port = $config['port'] ? $config['port'] : '3306'; 
  30.  $this->user = $config['user'] ? $config['user'] : 'root'; 
  31.  $this->pass = $config['pass'] ? $config['pass'] : ''; 
  32.  $this->data = $config['data'] ? $config['data'] : ''; 
  33.  $this->debug = $config["debug"] ? $config["debug"] : false; 
  34.  $this->prefix = $config['prefix'] ? $config['prefix'] : 'qinggan_'; 
  35.  if($this->data) 
  36.  { 
  37.   $ifconnect = $this->connect($this->data); 
  38.   if(!$ifconnect) 
  39.   { 
  40.   $this->conn = false; 
  41.   return false; 
  42.   } 
  43.  } 
  44.  return true; 
  45.  } 
  46.  #[兼容PHP4] 
  47.  function db_mysqli($config=array()) 
  48.  { 
  49.  return $this->__construct($config); 
  50.  } 
  51.  #[连接数据库] 
  52.  function connect($database="") 
  53.  { 
  54.  $start_time = $this->time_used(); 
  55.  if(!$this->port) $this->port = "3306"; 
  56.  $this->conn = @mysqli_connect($this->host,$this->user,$this->pass,"",$this->port) or false; 
  57.  if(!$this->conn) 
  58.  { 
  59.   return false; 
  60.  } 
  61.  $version = $this->get_version(); 
  62.  if($version>"4.1") 
  63.  { 
  64.   mysqli_query($this->conn,"SET NAMES 'utf8'"); 
  65.   if($version>"5.0.1") 
  66.   { 
  67.   mysqli_query($this->conn,"SET sql_mode=''"); 
  68.   } 
  69.  } 
  70.  $end_time = $this->time_used(); 
  71.  $this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间] 
  72.  $ifok = $this->select_db($database); 
  73.  return $ifok ? true : false; 
  74.  } 
  75.  function select_db($data="") 
  76.  { 
  77.  $database = $data ? $data : $this->data; 
  78.  if(!$database) 
  79.  { 
  80.   return false; 
  81.  } 
  82.  $this->data = $database; 
  83.  $start_time = $this->time_used(); 
  84.  $ifok = mysqli_select_db($this->conn,$database); 
  85.  if(!$ifok) 
  86.  { 
  87.   return false; 
  88.  } 
  89.  $end_time = $this->time_used(); 
  90.  $this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间] 
  91.  return true; 
  92.  } 
  93.  #[关闭数据库连接,当您使用持续连接时该功能失效] 
  94.  function close() 
  95.  { 
  96.  if(is_resource($this->conn)) 
  97.  { 
  98.   return mysqli_close($this->conn); 
  99.  } 
  100.  else 
  101.  { 
  102.   return true; 
  103.  } 
  104.  } 
  105.  function __destruct() 
  106.  { 
  107.  return $this->close(); 
  108.  } 
  109.  function set($name,$value) 
  110.  { 
  111.  if($name == "rs_type") 
  112.  { 
  113.   $value = strtolower($value) == "num" ? MYSQLI_NUM : MYSQLI_ASSOC; 
  114.  } 
  115.  $this->$name = $value; 
  116.  } 
  117.  function query($sql) 
  118.  { 
  119.  if(!is_resource($this->conn)) 
  120.  { 
  121.   $this->connect(); 
  122.  } 
  123.  else 
  124.  { 
  125.   if(!mysql_ping($this->conn)) 
  126.   { 
  127.    $this->close(); 
  128.    $this->connect(); 
  129.   } 
  130.  } 
  131.  if($this->debug) 
  132.  { 
  133.   $sqlkey = md5($sql); 
  134.   if($this->querylist) 
  135.   { 
  136.   $qlist = array_keys($this->querylist); 
  137.   if(in_array($sqlkey,$qlist)) 
  138.   { 
  139.    $count = $this->querylist[$sqlkey]["count"] + 1; 
  140.    $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>$count); 
  141.   }else{ 
  142.    $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1); 
  143.   } 
  144.   } 
  145.   else{ 
  146.   $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1); 
  147.   } 
  148.  } 
  149.  $start_time = $this->time_used(); 
  150.  $func = $this->unbuffered && function_exists("mysqli_multi_query") ? "mysqli_multi_query" : "mysqli_query"; 
  151.  $this->result = @$func($this->conn,$sql); 
  152.  $this->query_count++; 
  153.  $end_time = $this->time_used(); 
  154.  $this->query_times += round($end_time - $start_time,5);#[查询时间] 
  155.  if(!$this->result) 
  156.  { 
  157.   return false; 
  158.  } 
  159.  return $this->result; 
  160.  } 
  161.  function get_all($sql="",$primary="") 
  162.  { 
  163.  $result = $sql ? $this->query($sql) : $this->result; 
  164.  if(!$result) 
  165.  { 
  166.   return false; 
  167.  } 
  168.  $start_time = $this->time_used(); 
  169.  $rs = array(); 
  170.  $is_rs = false; 
  171.  while($rows = mysqli_fetch_array($result,$this->rs_type)) 
  172.  { 
  173.   if($primary && $rows[$primary]) 
  174.   { 
  175.   $rs[$rows[$primary]] = $rows; 
  176.   } 
  177.   else 
  178.   { 
  179.   $rs[] = $rows; 
  180.   } 
  181.   $is_rs = true; 
  182.  } 
  183.  $end_time = $this->time_used(); 
  184.  $this->query_times += round($end_time - $start_time,5);#[查询时间] 
  185.  return ($is_rs ? $rs : false); 
  186.  } 
  187.  function get_one($sql="") 
  188.  { 
  189.  $start_time = $this->time_used(); 
  190.  $result = $sql ? $this->query($sql) : $this->result; 
  191.  if(!$result) 
  192.  { 
  193.   return false; 
  194.  } 
  195.  $rows = mysqli_fetch_array($result,$this->rs_type); 
  196.  $end_time = $this->time_used(); 
  197.  $this->query_times += round($end_time - $start_time,5);#[查询时间] 
  198.  return $rows; 
  199.  } 
  200.  function insert_id($sql="") 
  201.  { 
  202.  if($sql) 
  203.  { 
  204.   $rs = $this->get_one($sql); 
  205.   return $rs; 
  206.  } 
  207.  else 
  208.  { 
  209.   return mysqli_insert_id($this->conn); 
  210.  } 
  211.  } 
  212.  function insert($sql) 
  213.  { 
  214.  $this->result = $this->query($sql); 
  215.  $id = $this->insert_id(); 
  216.  return $id; 
  217.  } 
  218.  function all_array($table,$condition="",$orderby="") 
  219.  { 
  220.  if(!$table) 
  221.  { 
  222.   return false; 
  223.  } 
  224.  $table = $this->prefix.$table; 
  225.  $sql = "SELECT * FROM ".$table; 
  226.  if($condition && is_array($condition) && count($condition)>0) 
  227.  { 
  228.   $sql_fields = array(); 
  229.   foreach($condition AS $key=>$value) 
  230.   { 
  231.   $sql_fields[] = "`".$key."`='".$value."' "; 
  232.   } 
  233.   $sql .= " WHERE ".implode(" AND ",$sql_fields); 
  234.  } 
  235.  if($orderby) 
  236.  { 
  237.   $sql .= " ORDER BY ".$orderby; 
  238.  } 
  239.  $rslist = $this->get_all($sql); 
  240.  return $rslist; 
  241.  } 
  242.  function one_array($table,$condition="") 
  243.  { 
  244.  if(!$table) 
  245.  { 
  246.   return false; 
  247.  } 
  248.  $table = $this->prefix.$table; 
  249.  $sql = "SELECT * FROM ".$table; 
  250.  if($condition && is_array($condition) && count($condition)>0) 
  251.  { 
  252.   $sql_fields = array(); 
  253.   foreach($condition AS $key=>$value) 
  254.   { 
  255.   $sql_fields[] = "`".$key."`='".$value."' "; 
  256.   } 
  257.   $sql .= " WHERE ".implode(" AND ",$sql_fields); 
  258.  } 
  259.  $rslist = $this->get_one($sql); 
  260.  return $rslist; 
  261.  } 
  262.  //将数组写入数据中 
  263.  function insert_array($data,$table,$insert_type="insert") 
  264.  { 
  265.  if(!$table || !is_array($data) || !$data) 
  266.  { 
  267.   return false; 
  268.  } 
  269.  $table = $this->prefix.$table;//自动增加表前缀 
  270.  if($insert_type == "insert") 
  271.  { 
  272.   $sql = "INSERT INTO ".$table; 
  273.  } 
  274.  else 
  275.  { 
  276.   $sql = "REPLACE INTO ".$table; 
  277.  } 
  278.  $sql_fields = array(); 
  279.  $sql_val = array(); 
  280.  foreach($data AS $key=>$value) 
  281.  { 
  282.   $sql_fields[] = "`".$key."`"; 
  283.   $sql_val[] = "'".$value."'"; 
  284.  } 
  285.  $sql.= "(".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")"; 
  286.  return $this->insert($sql); 
  287.  } 
  288.  //更新数据 
  289.  function update_array($data,$table,$condition) 
  290.  { 
  291.  if(!$data || !$table || !$condition || !is_array($data) || !is_array($condition)) 
  292.  { 
  293.   return false; 
  294.  } 
  295.  $table = $this->prefix.$table;//自动增加表前缀 
  296.  $sql = "UPDATE ".$table." SET "; 
  297.  $sql_fields = array(); 
  298.  foreach($data AS $key=>$value) 
  299.  { 
  300.   $sql_fields[] = "`".$key."`='".$value."'"; 
  301.  } 
  302.  $sql.= implode(",",$sql_fields); 
  303.  $sql_fields = array(); 
  304.  foreach($condition AS $key=>$value) 
  305.  { 
  306.   $sql_fields[] = "`".$key."`='".$value."' "; 
  307.  } 
  308.  $sql .= " WHERE ".implode(" AND ",$sql_fields); 
  309.  return $this->query($sql); 
  310.  } 
  311.  function count($sql="") 
  312.  { 
  313.  if($sql) 
  314.  { 
  315.   $this->rs_type = MYSQLI_NUM; 
  316.   $this->query($sql); 
  317.   $rs = $this->get_one(); 
  318.   $this->rs_type = MYSQLI_ASSOC; 
  319.   return $rs[0]; 
  320.  } 
  321.  else 
  322.  { 
  323.   return mysqli_num_rows($this->result); 
  324.  } 
  325.  } 
  326.  function num_fields($sql="") 
  327.  { 
  328.  if($sql) 
  329.  { 
  330.   $this->query($sql); 
  331.  } 
  332.  return mysqli_num_fields($this->result); 
  333.  } 
  334.  function list_fields($table) 
  335.  { 
  336.  $rs = $this->get_all("SHOW COLUMNS FROM ".$table); 
  337.  if(!$rs) 
  338.  { 
  339.   return false; 
  340.  } 
  341.  foreach($rs AS $key=>$value) 
  342.  { 
  343.   $rslist[] = $value["Field"]; 
  344.  } 
  345.  return $rslist; 
  346.  } 
  347.  #[显示表名] 
  348.  function list_tables() 
  349.  { 
  350.  $rs = $this->get_all("SHOW TABLES"); 
  351.  return $rs; 
  352.  } 
  353.  function table_name($table_list,$i) 
  354.  { 
  355.  return $table_list[$i]; 
  356.  } 
  357.  function escape_string($char) 
  358.  { 
  359.  if(!$char) 
  360.  { 
  361.   return false; 
  362.  } 
  363.  return mysqli_escape_string($this->conn,$char); 
  364.  } 
  365.  function get_version() 
  366.  { 
  367.  return mysqli_get_server_info($this->conn); 
  368.  } 
  369.  function time_used() 
  370.  { 
  371.  $time = explode(" ",microtime()); 
  372.  $used_time = $time[0] + $time[1]; 
  373.  return $used_time; 
  374.  } 
  375.  //Mysql的查询时间 
  376.  function conn_times() 
  377.  { 
  378.  return $this->conn_times + $this->query_times; 
  379.  } 
  380.  //MySQL查询资料 
  381.  function conn_count() 
  382.  { 
  383.  return $this->query_count; 
  384.  } 
  385.  # 高效SQL生成查询,仅适合单表查询 
  386.  function phpok_one($tbl,$condition="",$fields="*") 
  387.  { 
  388.  $sql = "SELECT ".$fields." FROM ".$this->db->prefix.$tbl; 
  389.  if($condition) 
  390.  { 
  391.   $sql .= " WHERE ".$condition; 
  392.  } 
  393.  return $this->get_one($sql); 
  394.  } 
  395.  function debug() 
  396.  { 
  397.  if(!$this->querylist || !is_array($this->querylist) || count($this->querylist) < 1) 
  398.  { 
  399.   return false; 
  400.  } 
  401.  $html = '<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#CECECE"><tr><td>'; 
  402.  $html.= '<table cellpadding="1" cellspacing="1" width="100%">'; 
  403.  $html.= '<tr><th bgcolor="#EFEFEF" height="30px">SQL</th><th bgcolor="#EFEFEF" width="80px">查询</th></tr>'; 
  404.  foreach($this->querylist AS $key=>$value) 
  405.  { 
  406.   $html .= '<tr><td bgcolor="#FFFFFF"><div style="padding:3px;color:#6E6E6E;">'.$value['sql'].'</div></td>'; 
  407.   $html .= '<td align="center" bgcolor="#FFFFFF"><div style="padding:3px;color:#000000;">'.$value["count"].'</div></td></tr>'; 
  408.  } 
  409.  $html.= "</table>"; 
  410.  $html.= "</td></tr></table>"; 
  411.  return $html; 
  412.  } 
  413.  function conn_status() 
  414.  { 
  415.  if(!$this->conn) return false; 
  416.  return true; 
  417.  } 
  418. } 
  419. ?> 

Tags: PHP连接mysqli

分享到: