<?php
class DBDA
{
public $host = "localhost"; //服務器地址
public $uid = "root"; //數據庫的用戶名
public $pwd = "324528"; //數據庫的密碼
//執行SQL語句,返回相應結果的函數
//$sql是要執行的SQL語句
//$type是SQL語句的類型,0代表增刪改,1代表查詢
//$db代表要操作的數據庫
public function Query($sql,$type=1,$db="mydb")
{
//造連接對象
$conn = new MySQLi($this->host,$this->uid,$this->pwd,$db);
//判斷連接是否成功
!mysqli_connect_error() or die("連接失敗!");
//執行SQL語句
$result = $conn->query($sql);
//判斷SQL語句類型
if($type==1)
{
//如果是查詢語句返回結果集的二維數組
return $result->fetch_all();
}
else
{
//如果是其他語句,返回true或false
return $result;
}
}
}
使用封裝類:引入DBDA (include("DBDA.php"))->實例化對象($db= new DBDA())->寫SQL語句($sql= "select * from DiaoYanTiMu";)->處理結果集($resultx=$db->Query($sql,$type=1);)->輸出結果。