: This article mainly introduces comments for mysql classes. For more information about PHP tutorials, see. Configuration file
Class
Conn = new mysqli ($ host, $ username, $ passwd); $ this-> selectDb ($ dbname); $ this-> coding ($ cod );} // SET the encoding format public function coding ($ cod) {mysql_query ("set character set ". $ cod);} // read a public function selectDb ($ dbname) {mysql_select_db ($ dbname, $ this-> conn );} // The last field to be queried: public function select ($ field = "*") {$ this-> SQL = "select ". $ field. "from ". $ this-> dataTable. $ this-> where. $ this-> limit;} // which table to use public function table ($ tableName) {$ this-> dataTable = $ tableName; return $ this ;} // execute the public function query () {$ res = mysql_query ($ this-> SQL); // The resource type indicates the query return result array if (is_resource ($ res )) {// returns a two-dimensional array while ($ row = mysql_fetch_assoc ($ res) {$ arr [] = $ row ;} // returns the one-dimensional array foreach ($ arr as $ key = >$ value) {$ ke = $ value;} return $ ke ;}} // Obtain the limit public function limit ($ limit) {$ this-> limit = 'limit '. $ limit; return $ this;} // where condition public function where ($ where) {$ this-> where = 'where '. $ where; return $ this;} // insert public function insert ($ insert) {// if it is an index array if ($ this-> is_assoc ($ insert )) {// Obtain the key of the array, that is, the field value $ key = array_keys ($ insert); $ value = array_values ($ insert); for ($ I = 0, $ j = 0; $ I
SQL = "insert ". $ this-> dataTable. "($ col) values ($ value)";} else {// if it is not an index array, convert the array directly to a string $ value = implode (',', $ insert); $ this-> SQL = "insert ". $ this-> dataTable. "values ($ value)" ;}}// modify public function update ($ update) {foreach ($ update as $ key =>$ value) {$ data. = $ key. "= ". $ value. ',';} $ values = rtrim ($ data, ','); $ this-> SQL = "UPDATE ". $ this-> dataTable. "SET $ values ". $ this-> where;} // delete Except public function delete () {$ this-> SQL = "DELETE FROM ". $ this-> dataTable. $ this-> where;} // determines whether the index array is public function is_assoc ($ array) {if (is_array ($ array) {$ keys = array_keys ($ array ); return $ keys! = Array_keys ($ keys);} return false ;}// $ B = array ('categoryid' => 9, 'name' => 11, 'description' => 22, 'priority '=> 35); // $ B = array (4, 5, 6, 7); // $ a = new MySqli (); // query // $ SQL = $ a-> table ("guagua_category")-> where ('categoryid = 1')-> limit (3)-> select (); // $ arr = $ a-> query (); // insert // $ SQL = $ a-> table ("guagua_category")-> insert ($ B ); // $ a-> query (); // modify // $ SQL = $ a-> table ("guagua_category")-> where ("CategoryID = 1 ") -> update ($ B); // $ a-> query (); // delete // $ SQL = $ a-> table ("guagua_category ") -> where ("CategoryID = 9")-> delete (); // $ a-> query ();
The above introduces the mysql class comments, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.