MySQL operation database for packaging implementation and additions and deletions to check function

Source: Internet
Author: User
Tags php class create database

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

Related Article

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.