- 相關(guān)推薦
PHP封裝數(shù)據(jù)庫(kù)操作類
而PHP在訪問(wèn)數(shù)據(jù)庫(kù)的時(shí)候又經(jīng)常會(huì)出現(xiàn)各種問(wèn)題,如字符編碼問(wèn)題、SQL語(yǔ)法錯(cuò)誤問(wèn)題、PHP處理數(shù)據(jù)記錄對(duì)象和返回對(duì)象的問(wèn)題等。下面內(nèi)容由小編為大家介紹PHP封裝數(shù)據(jù)庫(kù)操作類方法,供大家參考!
優(yōu)點(diǎn):
方便快捷, 數(shù)據(jù)庫(kù)操作只需調(diào)用接口;
統(tǒng)一編碼(utf8),不易導(dǎo)致亂碼
結(jié)構(gòu)清晰. 如處理前端請(qǐng)求的后臺(tái)程序(test.php) + 表封裝類(user.class.php) + 數(shù)據(jù)庫(kù)封裝類(db.class.php) + 配置信息(configuration.php)
以下例子有四個(gè)文件: configuration.php + db.class.php + user.class.php + test.php,放在同一個(gè)目錄下。
首先是一個(gè)數(shù)據(jù)庫(kù)配置的文件類configuration.php
/**
* 數(shù)據(jù)庫(kù)配置信息
*/
define('DB_HOST','localhost'); //服務(wù)器
define('DB_USER','root'); //數(shù)據(jù)庫(kù)用戶名
define('DB_PASSWORD',''); //數(shù)據(jù)庫(kù)密碼
define('DB_NAME','test0'); //默認(rèn)數(shù)據(jù)庫(kù)
define('DB_CHARSET','utf8'); //數(shù)據(jù)庫(kù)字符集
define('TIMEZONE',"PRC"); //時(shí)區(qū)設(shè)置
?>
接下來(lái)就是數(shù)據(jù)庫(kù)操作類db.class.php
require_once("./configuration.php"); //引入配置常量文件
date_default_timezone_set(TIMEZONE);
/**
* 類名:DB
* 說(shuō)明:數(shù)據(jù)庫(kù)操作類
*/
class
DB
{
public
$host; //服務(wù)器
public
$username; //數(shù)據(jù)庫(kù)用戶名
public
$password; //數(shù)據(jù)密碼
public
$dbname; //數(shù)據(jù)庫(kù)名
public
$conn; //數(shù)據(jù)庫(kù)連接變量
/**
* DB類構(gòu)造函數(shù)
*/
public
function DB($host=DB_HOST ,$username=DB_USER,$password=DB_PASSWORD,$dbname=DB_NAME)
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
}
/**
* 打開數(shù)據(jù)庫(kù)連接
*/
public
function open()
{
$this->conn = mysql_connect($this->host,$this->username,$this->password);
mysql_select_db($this->dbname);
mysql_query("SET CHARACTER SET utf8");
}
/**
* 關(guān)閉數(shù)據(jù)連接
*/
public
function close()
{
mysql_close($this->conn);
}
/**
* 通過(guò)sql語(yǔ)句獲取數(shù)據(jù)
* @return: array()
*/
public
function getObjListBySql($sql)
{
$this->open();
$rs
= mysql_query($sql,$this->conn);
$objList
= array();
while($obj
= mysql_fetch_object($rs))
{
if($obj)
{
$objList[] = $obj;
}
}
$this->close();
return
$objList;
}
/**
* 向數(shù)據(jù)庫(kù)表中插入數(shù)據(jù)
* @param:$table,表名
* @param:$columns,包含表中所有字段名的數(shù)組。默認(rèn)空數(shù)組,則是全部有序字段名
* @param:$values,包含對(duì)應(yīng)所有字段的屬性值的數(shù)組
*/
public
function Data($table,$columns=array(),$values=array())
{
$sql
= ' into '.$table
.'( ';
for($i
= 0; $i
< sizeof($columns);$i
++)
{
$sql
.= $columns[$i];
if($i
< sizeof($columns) - 1)
{
$sql
.= ',';
}
}
$sql
.= ') values ( ';
for($i
= 0; $i
< sizeof($values);$i
++)
{
$sql
.= "'".$values[$i]."'";
if($i
< sizeof($values) - 1)
{
$sql
.= ',';
}
}
$sql
.= ' )';
$this->open();
mysql_query($sql,$this->conn);
$id
= mysql__id($this->conn);
$this->close();
return
$id;
}
/**
* 通過(guò)表中的某一屬性獲取數(shù)據(jù)
*/
public
function getDataByAtr($tableName,$atrName,$atrValue){
@$data
= $this->getObjListBySql("SELECT * FROM ".$tableName." WHERE $atrName = '$atrValue'");
if(count($data)!=0)return
$data;
return
NULL;
}
/**
* 通過(guò)表中的"id",刪除記錄
*/
public
function ($tableName,$atrName,$atrValue){
$this->open();
$Result
= false;
if(mysql_query("DELETE FROM ".$tableName." WHERE $atrName = '$atrValue'")) $Result
= true;
$this->close();
if($Result) return
true;
else
return false;
}
/**
* 更新表中的屬性值
*/
public
function updateParamById($tableName,$atrName,$atrValue,$key,$value){
$db
= new
DB();
$db->open();
if(mysql_query("UPDATE ".$tableName." SET $key = '$value' WHERE $atrName = '$atrValue' ")){ //$key不要單引號(hào)
$db->close();
return
true;
}
else{
$db->close();
return
false;
}
}
/*
* @description: 取得一個(gè)table的所有屬性名
* @param: $tbName 表名
* @return:字符串?dāng)?shù)組
*/
public
function fieldName($tbName){
$resultName=array();
$i=0;
$this->open();
$result
= mysql_query("SELECT * FROM $tbName");
while
($property
= mysql_fetch_field($result)){
$resultName[$i++]=$property->name;
}
$this->close();
return
$resultName;
}
}
?>
接下來(lái)是測(cè)試了。我在phpmyadmin中建了一個(gè)test0數(shù)據(jù)庫(kù),里面建一張表user。然后用php寫一個(gè)user類對(duì)應(yīng)數(shù)據(jù)庫(kù)中的user表。
user.class.php
require_once("./db.class.php");
class
User{
public
$name = NULL;
public
$password = NULL;
/**
* 構(gòu)造函數(shù)
*/
public
function __construct($name,$password){
$this->name = $name;
$this->password = $password;
}
public
function (){
$db
= new
DB();
$resultid
= $db->Data("user",array(),array('',$this->name,$this->password));
return
$resultid;
}
public
static function getUserById($uid){
$db
= new
DB();
return
$db->getDataByAtr("user",'uid',$uid);
}
public
static function getUserByName($name){
$db
= new
DB();
@$data
= $db->getObjListBySql("SELECT * FROM user WHERE name = '$name'");
if(count($data)!=0)return
$data;
else
return null;
}
public
static function getAllUser(){
$db
= new
DB();
@$data
= $db->getObjListBySql("SELECT * FROM user");
if(count($data)!=0) return
$data;
else
return null;
}
public
static function ByUid($uid){
$admin
= Admin::getAdminById($uid);
$db
= new
DB();
if($db->("user","uid",$uid)) return
true;
else
return false;
}
}
?>
測(cè)試程序: test.php
header("Content-Type:text/html; charset=utf8");
require_once("./user.class.php");
$user
= new
User("HelloWorld","123456");
$user->();
$users
= User::getAllUser();
foreach
($users
as $u) {
echo
"
".$u->name."
".$u->password."
";
}
?>
運(yùn)行結(jié)果:
【PHP封裝數(shù)據(jù)庫(kù)操作類】相關(guān)文章:
PHP數(shù)據(jù)庫(kù)操作類-ezSQL08-19
PHP對(duì)數(shù)據(jù)庫(kù)MySQL的連接操作11-10
PHP操作MySQL數(shù)據(jù)庫(kù)的基本類10-14
PHP中FTP操作類代碼09-25
php備份數(shù)據(jù)庫(kù)類的方法09-04
關(guān)于php操作mysql執(zhí)行數(shù)據(jù)庫(kù)查詢08-11
PHP中如何定義類及其成員屬性與操作09-23
PHP文件怎么操作09-03
PHP中的Reload操作06-26