Complete example of mysqli encapsulated by php, and mysqli encapsulated by php

Source: Internet
Author: User

Complete example of mysqli encapsulated by php, and mysqli encapsulated by php

This example describes the mysqli class encapsulated by php. We will share this with you for your reference. The details are as follows:

Class:

<? Phpheader ('content-type: text/html; charset = UTF-8 ');/* master the necessary conditions to meet the singleton mode (1) private constructor-to prevent the object from being instantiated using the new keyword outside the class (2) Private member attributes-to prevent the attribute of the object to be imported outside the class (3) private cloning method-to prevent another object from being cloned outside the class (4) public static method-to allow users to operate on instantiated objects */class ConnectMysqli {// private Property private static $ dbcon = false; private $ host; private $ port; private $ user; private $ pass; private $ db; private $ charset; private $ link; // private constructor private 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']: 'root'; $ this-> db = $ config ['db']? $ Config ['db']: 'small2'; $ this-> charset = isset ($ arr ['charset'])? $ Arr ['charset']: 'utf8'; // connect to the database $ this-> db_connect (); // select database $ this-> db_usedb (); // set the character set $ this-> db_charset ();} // connect to the database private function db_connect () {$ this-> link = mysqli_connect ($ this-> host. ':'. $ this-> port, $ this-> user, $ this-> pass); if (! $ This-> link) {echo "Database Connection Failed <br>"; echo "error code ". mysqli_errno ($ this-> link ). "<br>"; echo "error message ". mysqli_error ($ this-> link ). "<br>"; exit ;}}// set the character set private function db_charset () {mysqli_query ($ this-> link, "set names {$ this-> charset}");} // select database private function db_usedb () {mysqli_query ($ this-> link, "use {$ this-> db}");} // private clone private function _ clone () {die ('clone is not allowed ');} // public static method public Static function getIntance () {if (self ::$ dbcon = false) {self ::$ dbcon = new self;} return self ::$ dbcon ;} // Method for executing SQL statements public function query ($ SQL) {$ res = mysqli_query ($ this-> link, $ SQL); if (! $ Res) {echo "SQL statement execution failed <br>"; echo "error code is ". mysqli_errno ($ this-> link ). "<br>"; echo "the error message is ". mysqli_error ($ this-> link ). "<br>" ;}return $ res ;}// print the public function p ($ arr) {echo "<pre>"; print_r ($ arr ); echo "</pre>";} public function v ($ arr) {echo "<pre>"; var_dump ($ arr); echo "</pre> ";} // obtain the last record id public function getInsertid () {return mysqli_insert_id ($ this-> link);}/*** query a field * @ param * @ re Turn string or int */public function getOne ($ SQL) {$ query = $ this-> query ($ SQL); return mysqli_free_result ($ query );} // get a row of records, return array one-dimensional array public function getRow ($ SQL, $ type = "assoc") {$ query = $ this-> query ($ SQL ); if (! In_array ($ type, array ("assoc", 'array', "row") {die ("mysqli_query error");} $ funcname = "mysqli_fetch _". $ type; return $ funcname ($ query) ;}// obtain a record. The precondition is to obtain a record through the resource. public function getFormSource ($ query, $ type = "assoc ") {if (! In_array ($ type, array ("assoc", "array", "row") {die ("mysqli_query error");} $ funcname = "mysqli_fetch _". $ type; return $ funcname ($ query);} // obtain multiple data records. Two-dimensional array public function getAll ($ SQL) {$ query = $ this-> query ($ SQL); $ list = array (); while ($ r = $ this-> getFormSource ($ query )) {$ list [] = $ r;} return $ list ;} /*** define the method for adding data * @ param string $ table name * @ param string orarray $ data [data] * @ return int the newly added id */pub Lic function insert ($ table, $ data) {// traverses the array to obtain the value of each field and field $ key_str = ''; $ v_str = ''; foreach ($ data as $ key = >$ v) {if (empty ($ v) {die ("error ");} // $ key value is the value of $ key_str for each field s. = $ key. ','; $ v_str. = "'$ V'," ;}$ key_str = trim ($ key_str,', '); $ v_str = trim ($ v_str ,','); // determine whether the data is empty $ SQL = "insert into $ table ($ key_str) values ($ v_str)"; $ this-> query ($ SQL ); // return the ID generated by the last addition operation. return $ this-> getInsertid ();}/** delete a piece of data Method * @ param1 $ table, $ where = array ('id' => '1') table Name condition * @ return Affected rows */public function deleteOne ($ table, $ where) {if (is_array ($ where) {foreach ($ where as $ key => $ val) {$ condition = $ key. '= '. $ val ;}} else {$ condition = $ where;} $ SQL = "delete from $ table where $ condition"; $ this-> query ($ SQL ); // return the number of affected rows return mysqli_affected_rows ($ this-> link);}/** Method for deleting multiple data entries * @ param1 $ table, $ where table name condition * @ Return number of affected rows */public function deleteAll ($ table, $ where) {if (is_array ($ where) {foreach ($ where as $ key => $ val) {if (is_array ($ val) {$ condition = $ key. 'In ('. implode (',', $ val ). ')';} else {$ condition = $ key. '= '. $ val ;}} else {$ condition = $ where;} $ SQL = "delete from $ table where $ condition"; $ this-> query ($ SQL ); // return the affected number of rows. return mysqli_affected_rows ($ this-> link);}/*** [modify des Cription] * @ param [type] $ table [table name] * @ param [type] $ data [data] * @ param [type] $ where [condition] * @ return [type] */public function update ($ table, $ data, $ where) {// traverse the array to obtain the value of each field and field $ str = ''; foreach ($ data as $ key => $ v) {$ str. = "$ key = '$ V'," ;}$ str = rtrim ($ str ,','); // modify the SQL statement $ SQL = "update $ table set $ str where $ where"; $ this-> query ($ SQL ); // return the affected number of rows. return mysqli_affected_rows ($ this-> link) ;}}?>

Usage test:

// Mysqli test $ db = ConnectMysqli: getIntance (); // var_dump ($ db);/* $ SQL = "select * from acticle "; $ list = $ db-> getAll ($ SQL); $ db-> p ($ list ); * // * $ SQL = "select * from acticle where acticle_id = 95"; $ list = $ db-> getRow ($ SQL ); $ db-> p ($ list); * // * $ SQL = "select title from acticle"; $ list = $ db-> getOne ($ SQL ); $ db-> p ($ list); No * // $ list = $ db-> insert ("users", $ _ POST ); // $ del = $ db-> deleteOne ("users", "id = 29"); // $ del = $ db-> deleteAll ("users ", "id in (27,28)"); // $ up = $ db-> update ("users", $ _ POST, "id = 27 "); // print_R ($ list );

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.