PHP joins class complete code instance _php technique in mysqli way

Source: Internet
Author: User
Tags joins mysql query

This article is an example of a database class in PHP that connects to a database in a mysqli way, which is sorted from a PHP CMS to implement PHP connection database classes, mysqli versions, compatible PHP4, For targeted friends, this code can be used to optimize and modify.

<?php #================================================================================================== # Filename:/db/db_mysqli.php # Note: Connect database class, mysqli version #===============================================================
 =================================== #[Class Library SQL] class Db_mysqli {var $query _count = 0;
 var $host;
 var $user;
 var $pass;
 var $data;
 var $conn;
 var $result;
 var $prefix = "Qinggan_";
 Returns the result set type, which defaults to the number + character var $rs _type = Mysqli_assoc;
 var $query _times = 0;#[query time] var $conn _times = 0;#[Connection Database time] var $unbuffered = false;
 Define the query list Var $querylist;
 var $debug = false;
 #[constructor] Function __construct ($config =array ()) {$this->host = $config [' Host ']? $config [' Host ']: ' localhost '; $this->port = $config [' Port ']?
 $config [' Port ']: ' 3306 '; $this->user = $config [' User ']?
 $config [' User ']: ' Root '; $this->pass = $config [' Pass ']?
 $config [' Pass ']: '; $this->data = $config [' Data ']?
 $config [' Data ']: '; $this->debug = $config ["Debug]"? $config["Debug": false; $this->prefix = $config [' prefix ']?
 $config [' prefix ']: ' Qinggan_ ';
  if ($this->data) {$ifconnect = $this->connect ($this->data);
  if (! $ifconnect) {$this->conn = false;
  return false;
 } return true;
 #[compatible PHP4] function db_mysqli ($config =array ()) {return $this->__construct ($config);
 #[Connection Database] Function connect ($database = "") {$start _time = $this->time_used ();
 if (! $this->port) $this->port = "3306";
 $this->conn = @mysqli_connect ($this->host, $this->user, $this->pass, "", $this->port) or false;
 if (! $this->conn) {return false;
 $version = $this->get_version ();
  if ($version > "4.1") {mysqli_query ($this->conn, "SET NAMES ' UTF8 '");
  if ($version > "5.0.1") {mysqli_query ($this->conn, "SET sql_mode=");
 }} $end _time = $this->time_used ();
 $this->conn_times + = round ($end _time-$start _time,5); #[time to connect to the database] $ifok = $this->select_db ($database); Return $ifok? true: False
 function select_db ($data = "") {$database = $data? $data: $this->data;
 if (! $database) {return false;
 } $this->data = $database;
 $start _time = $this->time_used ();
 $ifok = mysqli_select_db ($this->conn, $database);
 if (! $ifok) {return false;
 $end _time = $this->time_used ();
 $this->conn_times + = round ($end _time-$start _time,5); #[time to connect to the database] return true; #[shut down the database connection, when you use persistent connection this function fails] function close () {if (Is_resource ($this->conn)) {return Mysqli_close ($this->conn
 );
 else {return true;
 } function __destruct () {return $this->close (); } function set ($name, $value) {if ($name = = "Rs_type") {$value = Strtolower ($value) = = "num"?
 MYSQLI_NUM:MYSQLI_ASSOC;
 } $this-> $name = $value;
 } function Query ($sql) {if (!is_resource ($this->conn)) {$this->connect ();
   else {if (!mysql_ping ($this->conn)) {$this->close ();
  $this->connect (); } if ($this->debug) {$sqlkey = mD5 ($sql);
  if ($this->querylist) {$qlist = Array_keys ($this->querylist);
   if (In_array ($sqlkey, $qlist)) {$count = $this->querylist[$sqlkey] ["Count"] + 1;
  $this->querylist[$sqlkey] = array ("SQL" => $sql, "Count" => $count);
  }else{$this->querylist[$sqlkey] = array ("SQL" => $sql, "Count" =>1);
  } else{$this->querylist[$sqlkey] = array ("SQL" => $sql, "Count" =>1);
 }} $start _time = $this->time_used (); $func = $this->unbuffered && function_exists ("Mysqli_multi_query")?
 "Mysqli_multi_query": "Mysqli_query";
 $this->result = @ $func ($this->conn, $sql);
 $this->query_count++;
 $end _time = $this->time_used ();
 $this->query_times + = round ($end _time-$start _time,5); #[query time] if (! $this->result) {return false;
 return $this->result;
 function Get_all ($sql = "", $primary = "") {$result = $sql? $this->query ($sql): $this->result;
 if (! $result) {return false; } $start _time = $this->time_used ();
 $rs = Array ();
 $is _rs = false; while ($rows = Mysqli_fetch_array ($result, $this->rs_type)) {if ($primary && $rows [$primary]) {$rs [
  $primary]] = $rows;
  else {$rs [] = $rows;
 } $is _rs = true;
 $end _time = $this->time_used ();
 $this->query_times + + round ($end _time-$start _time,5) #[query time] return ($is _rs? $rs: false);
 function Get_one ($sql = "") {$start _time = $this->time_used (); $result = $sql?
 $this->query ($sql): $this->result;
 if (! $result) {return false;
 $rows = Mysqli_fetch_array ($result, $this->rs_type);
 $end _time = $this->time_used ();
 $this->query_times + + round ($end _time-$start _time,5); #[query time] return $rows;
  The function insert_id ($sql = "") {if ($sql) {$rs = $this->get_one ($sql);
 return $rs;
 else {return mysqli_insert_id ($this->conn);
 The function Insert ($sql) {$this->result = $this->query ($sql);
 $id = $this->insert_id ();
 return $id; } function AlL_array ($table, $condition = "", $orderby = "") {if (! $table) {return false;
 $table = $this->prefix. $table;
 $sql = "SELECT * from". $table;
  if ($condition && is_array ($condition) && count ($condition) >0) {$sql _fields = array (); foreach ($condition as $key => $value) {$sql _fields[] = "'. $key." ' = ' '. $value. "'
  ";
 $sql. = "WHERE" Implode ("and", $sql _fields);
 } if ($orderby) {$sql. = "ORDER by". $orderby;
 $rslist = $this->get_all ($sql);
 return $rslist;
 The function One_array ($table, $condition = "") {if (! $table) {return false;
 $table = $this->prefix. $table;
 $sql = "SELECT * from". $table;
  if ($condition && is_array ($condition) && count ($condition) >0) {$sql _fields = array (); foreach ($condition as $key => $value) {$sql _fields[] = "'. $key." ' = ' '. $value. "'
  ";
 $sql. = "WHERE" Implode ("and", $sql _fields);
 $rslist = $this->get_one ($sql);
 return $rslist; ///write array to data function INsert_array ($data, $table, $insert _type= "Insert") {if (! $table | |!is_array ($DATA) | |! $data) {return false;
 $table = $this->prefix. $table//Automatically increases the table prefix if ($insert _type = = "Insert") {$sql = "insert into". $table;
 else {$sql = ' REPLACE into '. $table;
 } $sql _fields = Array ();
 $sql _val = Array (); foreach ($data as $key => $value) {$sql _fields[] = "'. $key."
  `";
 $sql _val[] = "'". $value. "'"; $sql. = "( Implode (",", $sql _fields)) VALUES (". (
 Implode (",", $sql _val)). "
 return $this->insert ($sql); }//Update data function Update_array ($data, $table, $condition) {if (! $data | |! $table | |! $condition | | |!is_array ($DATA) | |!
 Is_array ($condition)) {return false; $table = $this->prefix. $table//Automatically increases the table prefix $sql = "UPDATE". $table. "
 SET ";
 $sql _fields = Array (); foreach ($data as $key => $value) {$sql _fields[] = "'. $key."
 ' = ' '. $value. "'";
 $sql. = Implode (",", $sql _fields);
 $sql _fields = Array (); foreach ($condition as $key => $value) {$sql _fields[] = "'. $key." ' = ' '. $value. "'
 ";
 $sql. = "WHERE" Implode ("and", $sql _fields);
 return $this->query ($sql);
  The function count ($sql = "") {if ($sql) {$this->rs_type = Mysqli_num;
  $this->query ($sql);
  $rs = $this->get_one ();
  $this->rs_type = MYSQLI_ASSOC;
 return $rs [0];
 else {return mysqli_num_rows ($this->result);
 }} function Num_fields ($sql = "") {if ($sql) {$this->query ($sql);
 Return Mysqli_num_fields ($this->result);
 The function List_fields ($table) {$rs = $this->get_all ("Show COLUMNS from". $table);
 if (! $rs) {return false;
 foreach ($rs as $key => $value) {$rslist [] = $value [Field];
 return $rslist;
 #[display table name] function list_tables () {$rs = $this->get_all ("Show Tables");
 return $rs;
 function table_name ($table _list, $i) {return $table _list[$i];
 The function escape_string ($char) {if (! $char) {return false;
 Return mysqli_escape_string ($this->conn, $char); } function Get_versIon () {return mysqli_get_server_info ($this->conn);
 function time_used () {$time = Explode ("", Microtime ());
 $used _time = $time [0] + $time [1];
 return $used _time;
 //mysql Query Time function conn_times () {return $this->conn_times + $this->query_times;
 //mysql Query data function Conn_count () {return $this->query_count; # Efficient SQL Build query, only for single table query function Phpok_one ($tbl, $condition = "", $fields = "*") {$sql = "select". $fields. "
 From ". $this->db->prefix. $tbl;
 if ($condition) {$sql. = "WHERE". $condition;
 return $this->get_one ($sql); 
  function Debug () {if (! $this->querylist | |!is_array ($this->querylist) | | | count ($this->querylist) < 1) {
 return false;
 $html = ' <table cellpadding= ' 0 "cellspacing=" 0 "width=" 100% "bgcolor=" #CECECE "><tr><td>";
 $html. = ' <table cellpadding= ' 1 "cellspacing=" 1 "width=" 100% ">"; $html. = ' <tr><th bgcolor= "#EFEFEF" height= "30px" >sql</th><th bgcolor= "#EFEFEF"Width= "80px" > Query </th></tr> '; foreach ($this->querylist as $key => $value) {$html. = ' <tr><td bgcolor= ' #FFFFFF ' ><div style= ' Paddi Ng:3px;color: #6E6E6E; " > '. $value [' SQL ']. '
  </div></td> '; $html. = ' <td align= ' center "bgcolor=" #FFFFFF "><div style=" Padding:3px;color: #000000; > '. $value [' count ']. '
 </div></td></tr> ';
 $html. = "</table>";
 $html. = "</td></tr></table>";
 return $html;
 The function Conn_status () {if (! $this->conn) return false;
 return true;
 }}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.