How PHP MySQL database encapsulates classes

Source: Internet
Author: User
Tags php mysql
This article demonstrates the code is a relatively simple database encapsulation class, more suitable for beginners, the need for friends can refer to the following

Then say a little more about the whole idea. The encapsulation of the entire class, containing a private property $conn and several manipulation functions that connect to the database. $conn when an object is instantiated, a resource-type connection handle is returned by the constructor when it processes the passed-in parameter. The database can then be modified by the corresponding method of invoking the instantiated object.

Talk less and show code:

<?php/** * The following code is used for the encapsulation of database operation classes * * @author rex<rex.sp.li@aliyun.com> * @version 1.0* @since 2015*/class mysql{// The database connection return value private $conn;/*** [constructor, return value to $conn]* @param [string] $hostname [hostname]* @param [string] $username [user name]* @param [str ing] $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 Administrator ';  Exit  } $this->conn = $conn;  $res = mysql_select_db ($dbname);  if (! $res) {echo ' connection failed, please contact Administrator ';  Exit } mysql_set_charset ($charset);} function __destruct () {mysql_close ();} /*** [GetAll gets all information]* @param [string] $sql [SQL statement]* @return [array] [returns a two-dimensional array]*/function getAll ($sql) {$result = Mysql_quer  Y ($sql, $this->conn);  $data = Array ();    if ($result && mysql_num_rows ($result) >0) {while ($row = Mysql_fetch_assoc ($result)) {$data [] = $row; }} return $data;} /*** [GetOne gets a single data]* @param [string] $sql [SQL statement]* @return [array] [returns a]*/function GetOne ($sql) {$result = mysql_query ($sql, $t  His->conn);  $data = Array ();  if ($result && mysql_num_rows ($result) >0) {$data = Mysql_fetch_assoc ($result); } return $data;} /*** [GetOne gets a single data]* @param [string] $table [table name]* @param [string] $data [by field name when key, property when key value of a one-dimensional array]* @return [Type] [return FALSE or insert number  According to id]*/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 updates database]* @param [string] $table [table name]* @param [array] $data [Updated data, by field name when key, property when key value of a one-dimensional array]* @param [string] $where [Condition, ' field name ' = ' field property ']* @return [type] [update successfully returns the number of rows affected, update failed to return false]*/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 delete data]* @param [string] $table [table name]* @param [string] $where [condition, ' field name ' = ' field property ']* @return [Type] [successfully returns the number of rows affected, failed to return  Back to False]*/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; }}}

Instantiate class:

<?php//contains the database operation class file include ' mysql.class.php ';//Set incoming parameter $hostname= ' localhost '; $username = ' root '; $password = ' 123456 ' ; $dbname = ' AISI '; $charset = ' UTF8 ';//Instantiate object $db = new Mysql ($hostname, $username, $password, $dbname);//Get a data $sql = " Select COUNT (as_article_id) as Count from As_article where as_article_type_id=1 "; $count = $db->getone ($sql);//Get more than one data  $sql = "SELECT * from As_article where as_article_type_id=1 the order by as_article_addtime desc limit $start, $limit"; $service = $db->getall ($sql);//Insert Data $arr = Array (' as_article_title ' = ' database Operation class ', ' as_article_author ' = ' Rex ',); $res = $ Db->insert (' as_article ', $arr);//Update Data $arr = Array (' as_article_title ' = ' instanced object ', ' as_article_author ' = ' Lee ') ,); $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);? >

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.