This article describes how PHP connects to a complete class code instance using mysqli, which is of great help for learning and understanding mysqli, for more information about how to connect to a database using mysqli in PHP, see this document. this database class is sorted out from a php cms, PHP can be used to connect to the database. MySQL is compatible with PHP4 and can be optimized and modified based on the code.
<? Php #=================================================== ========================================================== ================================#filename: /db/db_mysqli.php # Note: database connection class, 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 _"; // return result set Type. the default value is number + character var $ rs_type = MYSQLI_ASSOC; var $ query_times = 0; # [query time] var $ conn_times = 0; # [Database connection 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']: '000000'; $ 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 with PHP4] function db_mysqli ($ config = array ()) {return $ this->__ construct ($ config) ;}# [connect to the 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 ); # [database connection time] $ 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 ); # [Database connection time] return true;} # [disable database connection. This function is invalid when you use persistent connection] function close () {if (is_resource ($ this-> conn) {return mysqli_close ($ this-> conn) ;}else {return true ;}} function _ struct () {return $ this-> close ();} function set ($ name, $ value) {if ($ name = "rs_type") {$ value = strtolower ($ val Ue) = "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-> queryli St [$ 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 [$ 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 ". $ orderby;} $ rslist = $ this-> get_all ($ SQL); return $ rsl Ist;} 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 ;} // function insert_array ($ data, $ t Able, $ insert_type = "insert") {if (! $ Table |! Is_array ($ data) |! $ Data) {return false;} $ table = $ this-> prefix. $ table; // automatically add the table prefix if ($ insert_type = "insert") {$ SQL = "INSERT ". $ table;} else {$ SQL = "REPLACE ". $ 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-> ins Ert ($ 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 add 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);} 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 ("SH Ow 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 ;} // Mysql Query time function conn_times () {return $ this-> conn_times + $ this-> query_times;} // MySQL Query data function conn_count () {return $ this-> query_c Ount;} # efficient SQL query generation, applicable only to 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 ='
'; $ Html. ='
'; $ Html. ='
SQL |
Query |
'; Foreach ($ this-> querylist AS $ key => $ value) {$ html. ='
'. $ Value [' SQL '].' | '; $ Html. ='
'. $ Value ["count"].' |
';} $ Html. ="
"; $ Html. =" |
"; Return $ html;} function conn_status () {if (! $ This-> conn) return false; return true ;}}?>