PHP connection database, to achieve the most basic additions and deletions (object-oriented)

Source: Internet
Author: User

1. Create a mysql_class.php file and then create a MySQL class in the file and define the variables

1 2 3 4 5 6 7 8 9 10 11 <?php     class Mysql{         private $host;//服务器地址         private $root;//用户名         private $password;//密码         private $database;//数据库名                 //后面所提到的各个方法都放在这个类里         //...     } ?>

2. Initialize class by constructor function

1 2 3 4 5 6 7 function __construct($host,$root,$password,$database){     $this->host = $host;     $this->root = $root;     $this->password = $password;     $this->database = $database;     $this->connect(); }

For the Connect () method, the next step

3. Create a connection database and close the database method

1 2 3 4 5 6 7 8 9 function Connect () {       $this->conn = mysql_connect ($this->host, $this->root, $this- Password) or Die ( "DB connnection Error!" .mysql_error ()); &NBSP;&NBSP;&NBSP;&NBSP; mysql_select_db ($this->database, $this->conn); &NBSP;&NBSP;&NBSP;&NBSP; mysql_query ( }            function dbclose () {       mysql_close ($this->conn); }

4, encapsulating the mysql_query (), mysql_fetch_array (), mysql_num_rows () functions

1 2 3 4 5 6 7 8 9 All function query ($sql) {       return mysql_query ($sql); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   function myArray ($result) {       return mysql_fetch_array ($result); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   function rows ($result) {      return mysql_num_rows ($result); }

5. Custom query Data method

1 2 3 function select($tableName,$condition){     return $this->query("SELECT * FROM $tableName $condition"); }

6. Custom Insert Data method

1 2 3 function insert($tableName,$fields,$value){     $this->query("INSERT INTO $tableName $fields VALUES$value"); }

7. Custom Modify Data method

1 2 3 function update($tableName,$change,$condition){     $this->query("UPDATE $tableName SET $change $condition"); }

8. Custom Delete Data method

1 2 3 function delete($tableName,$condition){     $this->query("DELETE FROM $tableName $condition"); }

Now that the database operations classes are encapsulated, let's look at how to use them.

We use or in the PHP connection database, to achieve the most basic additions and deletions (process-oriented) in the article involved in the Database and table (table data added itself):

9, then we first instantiate the database operation class

1 $db = new Mysql("localhost","root","admin","beyondweb_test");

Instantiation can be done outside of the MySQL class in the mysql_class.php file.

Then we create a test.php file, first introduce the mysql_class.php file

1 2 3 <?php     require("mysql_class.php"); ?>

And then we'll start working on it.

10. Inserting data into the table

1 2 3 4 <?php     $insert = $db->insert("user","(nikename,email)","(#beyondweb#,#[email protected]#)");//请把#号替换为单引号     $db->dbClose(); ?>

11. Modify the data in the table

1 2 3 4 <?php     $update = $db->update("user","nikename = #beyondwebcn#","where id = #2#");//请把#号替换为单引号     $db->dbClose(); ?>

12. Query the data in the table and output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?php     $select = $db->select("user");     $row = $db->rows($select);     if($row>=1){ ?> <table border="1px">     <tr>         <th>id</th>         <th>nikename</th>         <th>email</th>     </tr> <?php     while($array = $db->myArray($select)){         echo "<tr>";         echo "<td>".$array[#id#]."</td>";//请把#号替换为单引号         echo "<td>".$array[#nikename#]."</td>";//请把#号替换为单引号         echo "<td>".$array[#email#]."</td>";//请把#号替换为单引号         echo "</tr>";     } ?> </table> <?php     }else{         echo "查不到任何数据!";     }          $db->dbClose(); ?>

13. Delete data from the table

1 2 3 4 <?php &NBSP;&NBSP;&NBSP;&NBSP; $delete = $db->delete ( "where nikename = #beyondweb #" )//Please replace the # with a single quote &NBSP;&NBSP;&NBSP;&NBSP; $db->dbclose (); ?>
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.