PHP鏈接數據庫,實現最基本的增刪改查(面向對象)

一、建立mysql_class.php文件而後在該文件中建立Mysql類,並定義變量php

1
2
3
4
5
6
7
8
9
10
11
<?php
     class Mysql{
         private $host;//服務器地址
         private $root;//用戶名
         private $password;//密碼
         private $database;//數據庫名
         
         //後面所提到的各個方法都放在這個類裏
         //...
     }
?>

二、經過構造函數初始化類css

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();
}

對於connect()方法,下一步再說mysql

三、建立鏈接數據庫及關閉數據庫方法web

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());
     mysql_select_db($this->database,$this->conn);
     mysql_query( "set names utf8" );
}
         
function dbClose(){
     mysql_close($this->conn);
}

四、對mysql_query()、mysql_fetch_array()、mysql_num_rows()函數進行封裝sql

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

五、自定義查詢數據方法數據庫

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

六、自定義插入數據方法服務器

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

七、自定義修改數據方法函數

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

八、自定義刪除數據方法fetch

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

如今,數據庫操做類已經封裝好了,下面咱們就來看看該怎麼使用。ui

咱們用的仍是在PHP鏈接數據庫,實現最基本的增刪改查(面向過程)一文中所涉及到的數據庫及表(表中數據本身添加):

九、那麼咱們先對數據庫操做類進行實例化

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

實例化能夠在mysql_class.php文件中的Mysql類以外進行。

而後咱們再建立一個test.php文件,首先把mysql_class.php文件引入

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

而後咱們就開始操做吧

十、向表中插入數據

1
2
3
4
<?php
     $insert = $db->insert( "user" , "(nikename,email)" , "(#beyondweb#,#beyondwebcn@xx.com#)" );//請把#號替換爲單引號
     $db->dbClose();
?>

十一、修改表中數據

1
2
3
4
<?php
     $update = $db->update( "user" , "nikename = #beyondwebcn#" , "where id = #2#" );//請把#號替換爲單引號
     $db->dbClose();
?>

十二、查詢表中數據並輸出

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();
?>

1三、刪除表中數據

1
2
3
4
<?php
     $delete = $db->delete( "user" , "where nikename = #beyondweb#" );//請把#號替換爲單引號
     $db->dbClose();
?>
相關文章
相關標籤/搜索