Php mysql database operation class (instance description), mysql instance description

Source: Internet
Author: User
Tags php mysql

Php mysql database operation class (instance description), mysql instance description

Next, let's talk about the overall idea. Encapsulation of the entire class, including a private attribute $ conn for connecting to the database and several operation functions. $ Conn: when an object is instantiated, a resource-based connection handle is returned after the constructor processes the input parameters. Then, you can add, query, modify, and delete databases by calling the corresponding method of the instantiated object.

Talk less and show code:

<? Php/*** the following code is used to encapsulate the database operation class ** @ author rex <rex.sp.li@aliyun.com> * @ version 1.0 * @ since 2015 */class Mysql {// database connection return value private $ conn; /*** [constructor, returns $ conn] * @ param [string] $ hostname [host name] * @ param [string] $ username [user name] * @ param [string] $ password [password] * @ param [string] $ dbname [database name] * @ param [string] $ charset [character set] * @ return [null] */function _ construct ($ hostname, $ username, $ password, $ dbname, $ charset = 'utf8') {$ Conn = @ mysql_connect ($ hostname, $ username, $ password); if (! $ Conn) {echo 'Connection failed. Please contact the Administrator '; exit;} $ this-> conn = $ conn; $ res = mysql_select_db ($ dbname); if (! $ Res) {echo 'Connection failed. Please contact the Administrator '; exit;} mysql_set_charset ($ charset);} function _ destruct () {mysql_close ();} /*** [getAll get all information] * @ param [string] $ SQL [SQL statement] * @ return [array] [returns a two-dimensional array] */function getAll ($ SQL) {$ result = mysql_query ($ SQL, $ this-> conn); $ data = array (); if ($ result & mysql_num_rows ($ result)> 0) {while ($ row = mysql_fetch_assoc ($ result) {$ data [] = $ row ;}} return $ data ;} /*** [getOne obtains a single piece of data] * @ param [string] $ SQL [SQL statement] * @ return [array] [returns a one-dimensional array] */function getOne ($ SQL) {$ result = mysql_query ($ SQL, $ this-> conn); $ data = array (); if ($ result & mysql_num_rows ($ result)> 0) {$ data = mysql_fetch_assoc ($ result);} return $ data ;} /*** [getOne obtains a single piece of data] * @ param [string] $ table [table name] * @ param [string] $ data [uses the field name as the key, attribute is a one-dimensional array of key values] * @ return [type] [returns false or the id of the inserted data] */function insert ($ table, $ data) {$ str = ''; $ str. = "insert into '$ table'"; $ str. = "('". implode ("','", array_keys ($ data )). "')"; $ str. = "VALUES"; $ str. = "('". implode ("','", $ data ). "')"; $ res = mysql_query ($ str, $ this-> conn); if ($ res & mysql_affected_rows ()> 0) {return mysql_insert_id ();} else {return false ;}} /*** [update database] * @ param [string] $ table [table name] * @ param [array] $ data [updated data, with the field name as the key, attribute is a one-dimensional array of the key value] * @ param [string] $ where [condition, 'Field name' = 'field attribute'] * @ return [type] [number of affected rows returned after successful update, false if update fails] */function update ($ table, $ data, $ where) {$ SQL = 'update '. $ table. 'set'; foreach ($ data as $ key => $ value) {$ SQL. = "'{$ key}' = '{$ value}'," ;}$ SQL = rtrim ($ SQL, ','); $ SQL. = "WHERE $ where"; $ res = mysql_query ($ SQL, $ this-> conn); if ($ res & mysql_affected_rows () {return mysql_affected_rows ();} else {return false;}/*** [delete data deletion] * @ param [string] $ table [table name] * @ param [string] $ where [condition, 'Field name' = 'field attribute'] * @ return [type] [The number of affected rows is returned successfully, and false is returned if a failure occurs] */function del ($ table, $ where) {$ SQL = "delete from '{$ table}' WHERE {$ where}"; $ res = mysql_query ($ SQL, $ this-> conn ); if ($ res & mysql_affected_rows () {return mysql_affected_rows () ;}else {return false ;}}}

Instantiation class:

<? Php // contains the database operation File include 'mysql. class. php '; // set the input parameter $ hostname = 'localhost'; $ username = 'root'; $ password = '000000'; $ dbname = 'asi '; $ charset = 'utf8'; // instantiated object $ db = new Mysql ($ hostname, $ username, $ password, $ dbname ); // get a piece of data $ SQL = "SELECT count (as_article_id) as count FROM as_article where as_article_type_id = 1"; $ count = $ db-> getOne ($ SQL ); // obtain multiple data records $ SQL = "SELECT * FROM as_article where as_article_type_id = 1 order B Y as_article_addtime desc limit $ start, $ limit "; $ service = $ db-> getAll ($ SQL ); // insert data $ arr = array ('as _ article_title '=> 'database operation class', 'as _ article_author' => 'recore ',); $ res = $ db-> insert ('as _ article', $ arr); // update data $ arr = array ('as _ article_title '=> 'instantiate object ', 'As _ article_author '=> 'lil',); $ where = "as_article_id = 1"; $ res = $ db-> update ('as _ article', $ arr, $ where); // delete data $ where = "as_article_id = 1"; $ res = $ db-> del ('as _ article' , $ Where);?>

After the Demo code, let's say a few words.

The $ SQL statement passed in by the getOne method is used to query a single piece of data and return a one-dimensional array. The getAll method is also used to input an SQL statement to query multiple pieces of data and return a two-dimensional array; the insert method is used to input the table name and associated array, and the boolen type or the index corresponding to the inserted data are returned. The update method is used to input the table name, associated array, and condition, and the boolen or affected number of rows is returned; the del method is used to input the table name and condition, and the boolen type is returned.

That's all, but not the all. If you are interested, you can use getOne and getAll to directly input SQL statements as parameters and then optimize them.

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.