today's PHP operations database, mostly with MYSQLI implementation, and in the implementation of MYSQLI operation database, in the e-commerce management, is also very easy to use, since the use of the frequency is so high, it has to be a function library encapsulation, there is no class method to encapsulate, or simply using a structure-oriented approach, a unified summary of:
(1) Connect to the database:
function Connect () { @ $link = Mysqli_connect (DB_HOST,DB_USER,DB_PWD);//Connect to Database Mysqli_set_charset ($link, db_ CHARSET);//Set Database font format mysqli_select_db ($link, Db_dbname) or Die (' database open failed ');//Select Database if (mysqli_connect_errno ()) {die (' Database connection failed: '. Mysqli_connect_errno ()); } return $link;}
(2) Inserting data
function Insert ($link, $table, $array) { $keys = join (', ', Array_keys ($array)); $values = "'". Join (', ', Array_values ($array)). "'"; $sql = ' insert INTO {$table} ({$keys}) VALUES ({$values}) '; Mysqli_query ($link, $sql); Return mysqli_insert_id ($link);}
(3) Modify the data
function Update ($link, $table, $array, $where = null) { $setstr = '; foreach ($array as $key = = $value) { $setstr. = ('. $key. ') = '. $value. ' '); if (! $setstr) { $sep = '; } else{ $sep = ', '; } $setstr. = $sep. $key. ' ='."'" $value "'"; } $sql = "Update {$table} set {$setstr}". ($where? ' WHERE '. $where: '); Mysqli_query ($link, $sql); Return Mysqli_affected_rows ($link); }
(4) Delete data
function Delete ($link, $table, $where = null) { $sql = "Delete from {$table}". $where? ' where '. $where: '); Mysqli_query ($link, $sql); Return Mysqli_affected_rows ($link); }
(5) Find a record
function Fetchone ($link, $sql, $result _type=mysqli_assoc) { $result = Mysqli_query ($link, $sql); $row = Mysqli_fetch_array ($result, $result _type); return $row;}
(6) Find more than one record
function Fetchall ($link, $sql, $result _type=mysqli_assoc) { $result = Mysqli_query ($link, $sql); while (@ $row = Mysqli_fetch_array ($result, $result _type)) { $row [] = $row; } return $row;}
(7) Returns the number of rows in the result set
function getresultnum ($link, $sql) {$result = Mysqli_query ($link, $sql); Return Mysqli_num_rows ($result);}