"Summary" CRUD operations encapsulation for MySQL extension library and mysqli extension library

Source: Internet
Author: User

At this stage PHP to operate MySQL database PHP to provide us with 3 sets of libraries

1. mysql extension library for process operation

2. Mysqli expansion Library object-oriented operation and process-oriented operation coexist security and efficiency higher than MySQL extension library

3. PDO expansion Library object-oriented operation

Today this post is mainly about MySQL extension library and mysqli expansion library is mainly recorded 2 sets of crud operation of the sub-assembly

The following code snippet is about the encapsulation of the Mysqli extension library about CRUD operations

Header ("Content-type:text/html;charset=utf-8"), class Sqlitool{private $mysqli;p rivate static $host = "localhost"; private static $user = "root";p rivate static $pwd =123456;private static $db = "Test";p ublic function __construct () {$this- >mysqli=new mysqli (self:: $host, Self:: $user, Self:: $pwd, Self:: $DB); if ($this->mysqli->connect_error) {die ( "Link failed". $this->mysqli->connect_error);} Sets the character set $this->mysqli->query ("Set names UTF8") that accesses the database; Public Function Execute_dql ($sql) {$res = $this->mysqli->query ($sql) or Die ("Operation failed". $this->mysqli->error); return $res;} Public Function Execute_dml ($sql) {$res = $this->mysqli->query ($sql) or Die ("Operation failed". $this->mysqli->error); if (! $res) {return 0;  Operation failed}else{if ($this->mysqli->affected_rows>0) {return 1;//indicates success}else{return 2;//indicates no row received effect}}}}

The following code snippet is about the package for the MySQL extension library about CRUD operations

Header ("Content-type:text/html;charset=utf-8");
Class Sqltool{private $con;p rivate $host = "localhost";p rivate $user = "root";p rivate $password =123456;private $db = "Test ";//constructor Initialize database operation link and set character Set function Sqltool () {$this->con=mysql_connect ($this->host, $this->user, $this- password); if (! $this->con) {die ("link Database error". Mysql_error ());} mysql_select_db ($this->db, $this->con); mysql_query ("Set names UTF8");} Complete the Query task Yes Public Function EXEUTE_DQL ($sql) {$res =mysql_query ($sql) or Die (Mysql_error ()); return $res;} Represents the Insert Update Deletepublic function exeuct_dml ($sql) {$b =mysql_query ($sql, $this->con) or Die (Mysql_error ()); (! $b) {return 0;//failed}else{if (mysql_affected_rows ($this->con) >0) {return 1;//indicates success}else{return 2;//indicates no row count affected}}}
}

  

"Summary" CRUD operations encapsulation for MySQL extension library and mysqli extension library

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.