SqlTool.class.php
<?php class sqltool{private $conn; Private $host = "localhost"; Private $user = "root"; Private $password = "root"; Private $db = "test1"; /* How to construct the connection database */function SqlTool () {$this->conn = mysql_connect ($this->host, $ This->user, $this->password); if (! $this->conn) {die (' connection failed '. Mysql_error ()); } mysql_select_db ($this->db, $this->conn); mysql_query (' Set names GBK '); }//select function Execute_dql ($sql) {$res = mysql_query ($sql, $this->conn); return $res; }//insert, update, delete function Execute_dml ($sql) {$obj = mysql_query ($sql, $this->conn); if (! $obj) {//return 0;//operation failed die (' operation failed '. Mysql_error ()); }else{if (mysql_affected_rows ($this->conn) >0) {//return 1;//Operation successful Echo "Operation succeeded"; }else{//return 2;//Number of rows did not receive impact die (' number of rows not affected '); }}}}?>
sqltooltest.php
<?php//Introduce the database class file require_once "SqlTool.class.php"; ----------------DML operation------------------//insert//$sql = "INSERT into user1 (name, password, e-mail, age) VALUES (' John Doe ', MD5 (' 123 '), ' [email protected] ', 18) "; Delete//$sql = "Delete from User1 where id = 9"; Update//$sql = "Update user1 set id=4 where name= ' John Doe '"; Create a SqlTool object//$SqlTool = new SqlTool (); $res = $SqlTool->execute_dml ($sql); --------------------DQL operation--------------------$sql = "SELECT * from User1"; Create a SqlTool object $SqlTool = new SqlTool (); $res = $SqlTool->execute_dql ($sql); while ($row =mysql_fetch_row ($res)) {foreach ($row as $key = + $val) {echo "--$val"; } echo "<br>"; } mysql_free_result ($res); /*if ($res ==0) {die (' operation failed '. Mysql_error ()); }else if ($res ==1) {echo "Operation succeeded"; }else if ($res ==2) {echo "line number is not affected"; }*/?>
Create a database
Create DATABASE test1;
Create a data table
create table user1(id int auto_increment primary key,name varchar(32) not null,password varchar(64) not null,email varchar(128) not null,age tinyint unsigned not null);
Table structure
Picture results for subsequent operations:
MySQL operation database for packaging implementation and additions and deletions to check function