PHPmysqli database operations

Source: Internet
Author: User
: This article mainly introduces PHPmysqli database operations. if you are interested in the PHP Tutorial, please refer to it.
 ObjectStatic private $ _ instance; // class instance // Constructor initialization $ mysqliObjectPrivate function _ construct () {$ this-> mysqli = new mysqli ($ this-> host, $ this-> user, $ this-> pwd, $ this-> db, $ this-> port); if (mysqli_connect_errno () {$ this-> mysqli = false; echo mysqli_connect_error (); die ();} else {$ this-> mysqli-> set_charset ($ char) ;}// destructor: release the result set and close the public function _ destruct () of the database () {$ this-> free (); $ this-> close () ;}// initialize $ mysqliObjectPublic static function getInstance () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self ();} return self ::$ _ instance ;} // release the result set private function free () {@$ this-> result-> free () ;}// close the database connection private function close () {$ this-> mysqli-> close () ;}// execute the SQL statement public function query ($ SQL) {return $ this-> mysqli-> query ($ SQL);} // Obtain the query result public function get_result_array ($ table, $ field, $ c $ table = $ this-> prefix. $ table; if (is_array ($ field) {$ field = join (',', $ Field) ;}$ SQL = "SELECT $ field FROM". $ table; if (! Empty ($ condition) $ SQL. = "$ condition"; $ this-> result = $ this-> query ($ SQL); $ return = array (); while ($ row = $ this-> fetch ($ this-> result) {$ return [] = $ row;} $ this-> free (); return $ return;} // add, delete, and modify public function execute ($ table, $ action, $ arr_field = array (), $ c $ table = $ this-> prefix. $ table; switch ($ action) {case 'insert': $ str_field = ''; $ str_val =''; foreach ($ arr_field as $ key => $ val) {$ str_field. = '''. $ key. '', '; $ Str_val. = '\''. $ val. '\', ';} $ str_field = rtrim ($ str_field,', '); $ str_val = rtrim ($ str_val ,','); $ SQL = "insert into $ table ($ str_field) VALUES ($ str_val)"; break; case 'delete': $ SQL = "DELETE FROM $ table"; if (! Empty ($ condition) $ SQL. = "WHERE $ condition"; break; case 'update': $ str_field = ''; foreach ($ arr_field as $ key => $ val) {$ str_field. = '''. $ key. "'=' $ val '," ;}$ str_field = rtrim ($ str_field,', '); $ SQL = "UPDATE $ table SET $ str_field"; if (! Empty ($ condition) $ SQL. = "WHERE $ condition"; break;} $ this-> query ($ SQL); return $ this-> get_affected_rows ();} // Obtain the number of affected rows (for addition, deletion, and modification operations) public function get_affected_rows () {return $ this-> mysqli-> affected_rows;} // Obtain the number of public function get_rows ($ table, $ c 1 ', $ id = 'id') {$ table = $ this-> prefix. $ table; $ SQL = "SELECT COUNT ($ id) num FROM ". $ table. "$ condition"; $ this-> result = $ this-> query ($ SQL); $ row = $ this-> fetch ($ this-> result ); return $ row ['num'];} // obtain the result set public function fetch ($ result) {return $ result-> fetch_array ($ this-> fetch_mode );} // Obtain all result sets public function fetch_all ($ result) {$ rows = array (); while ($ row = $ this-> fetch ($ result )) {$ rows [] = $ row;} return $ rows ;}}

A database operation class encapsulated by the interface for writing mobile apps with PHP

The above introduces the PHP mysqli database operation class, including the object content, hope to be helpful to friends who are interested in PHP tutorials.

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.