PHP mysqli Mode Connection class

Source: Internet
Author: User

Share a PHP mysqli connection class complete code instance, about MYSQLI usage instance.

A database class instance that joins the database in PHP in Mysqli way, the database class is organized from a PHP cms, can implement PHP connection database class, mysqli version, compatible PHP4, for the targeted needs of friends can be optimized and modified according to this code.
<?php
#==================================================================================================
# Filename:/db/db_mysqli.php
# Note: Connect to the database class, version mysqli
#==================================================================================================
#[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 is the number + character by default
var $rs _type = Mysqli_assoc;
var $query _times = 0;#[query Time]
var $conn _times = 0;#[Connection Database time]
var $unbuffered = false;
Define a 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 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 database]
return true;
}
#[Close the database connection, which fails when you use persistent connections]
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))
{(Script Academy www.jbxue.com)
if ($primary && $rows [$primary])
{
$rs [$rows [$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;
}
function insert_id ($sql = "")
{
if ($sql)
{
$rs = $this->get_one ($sql);
return $rs;
}
Else
{
Return mysqli_insert_id ($this->conn);
}
}
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;
}
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;
}
Writes an array to the data
function Insert_array ($data, $table, $insert _type= "Insert")
{
if (! $table | |!is_array ($DATA) | |! $data)
{
return false;
}
$table = $this->prefix. $table;//auto-increment 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;//auto-increment 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);
}
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);
}
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];
}
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;
}
Query time for MySQL
function Conn_times ()
{
return $this->conn_times + $this->query_times;
}
MySQL Query information
function Conn_count ()
{
return $this->query_count;
}
# Efficient SQL Build query for single-table queries only
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 " > Enquiry </th></tr> ';
foreach ($this->querylist as $key = $value)
{
$html. = ' <tr><td bgcolor= ' #FFFFFF "><div style=" Padding: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;
}
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.