PHP封裝類(lèi)似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類(lèi)與簡(jiǎn)單應(yīng)用示例
本文實(shí)例講述了PHP封裝類(lèi)似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類(lèi)與簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:
<?php
header("Content-Type:text/html;charset=utf-8");
/**
*php操作mysql的工具類(lèi)
*/
class Db{
private $_db = null;//數(shù)據(jù)庫(kù)連接句柄
private $_table = null;//表名
private $_where = null;//where條件
private $_order = null;//order排序
private $_limit = null;//limit限定查詢(xún)
private $_group = null;//group分組
private $_configs = array(
'hostname' => 'localhost',
'dbname' => 'test',
'username' => 'root',
'password' => '1234'
);//數(shù)據(jù)庫(kù)配置
/**
* 構(gòu)造函數(shù),連接數(shù)據(jù)庫(kù)
*/
public function __construct(){
$link = $this->_db;
if(!$link){
$db = mysqli_connect($this->_configs['hostname'],$this->_configs['username'],$this->_configs['password'],$this->_configs['dbname']);
mysqli_query($db,"set names utf8");
if(!$db){
$this->ShowException("錯(cuò)誤信息".mysqli_connect_error());
}
$this->_db = $db;
}
}
/**
* 獲取所有數(shù)據(jù)
*
* @param <type> $table The table
*
* @return boolean All.
*/
public function getAll($table=null){
$link = $this->_db;
if(!$link)return false;
$sql = "SELECT * FROM {$table}";
$data = mysqli_fetch_all($this->execute($sql));
return $data;
}
public function table($table){
$this->_table = $table;
return $this;
}
/**
* 實(shí)現(xiàn)查詢(xún)操作
*
* @param string $fields The fields
*
* @return boolean ( description_of_the_return_value )
*/
public function select($fields="*"){
$fieldsStr = '';
$link = $this->_db;
if(!$link)return false;
if(is_array($fields)){
$fieldsStr = implode(',', $fields);
}elseif(is_string($fields)&&!empty($fields)){
$fieldsStr = $fields;
}
$sql = "SELECT {$fields} FROM {$this->_table} {$this->_where} {$this->_order} {$this->_limit}";
$data = mysqli_fetch_all($this->execute($sql));
return $data;
}
/**
* order排序
*
* @param string $order The order
*
* @return boolean ( description_of_the_return_value )
*/
public function order($order=''){
$orderStr = '';
$link = $this->_db;
if(!$link)return false;
if(is_string($order)&&!empty($order)){
$orderStr = "ORDER BY ".$order;
}
$this->_order = $orderStr;
return $this;
}
/**
* where條件
*
* @param string $where The where
*
* @return <type> ( description_of_the_return_value )
*/
public function where($where=''){
$whereStr = '';
$link = $this->_db;
if(!$link)return $link;
if(is_array($where)){
foreach ($where as $key => $value) {
if($value == end($where)){
$whereStr .= "`".$key."` = '".$value."'";
}else{
$whereStr .= "`".$key."` = '".$value."' AND ";
}
}
$whereStr = "WHERE ".$whereStr;
}elseif(is_string($where)&&!empty($where)){
$whereStr = "WHERE ".$where;
}
$this->_where = $whereStr;
return $this;
}
/**
* group分組
*
* @param string $group The group
*
* @return boolean ( description_of_the_return_value )
*/
public function group($group=''){
$groupStr = '';
$link = $this->_db;
if(!$link)return false;
if(is_array($group)){
$groupStr = "GROUP BY ".implode(',',$group);
}elseif(is_string($group)&&!empty($group)){
$groupStr = "GROUP BY ".$group;
}
$this->_group = $groupStr;
return $this;
}
/**
* limit限定查詢(xún)
*
* @param string $limit The limit
*
* @return <type> ( description_of_the_return_value )
*/
public function limit($limit=''){
$limitStr = '';
$link = $this->_db;
if(!$link)return $link;
if(is_string($limit)||!empty($limit)){
$limitStr = "LIMIT ".$limit;
}elseif(is_numeric($limit)){
$limitStr = "LIMIT ".$limit;
}
$this->_limit = $limitStr;
return $this;
}
/**
* 執(zhí)行sql語(yǔ)句
*
* @param <type> $sql The sql
*
* @return boolean ( description_of_the_return_value )
*/
public function execute($sql=null){
$link = $this->_db;
if(!$link)return false;
$res = mysqli_query($this->_db,$sql);
if(!$res){
$errors = mysqli_error_list($this->_db);
$this->ShowException("報(bào)錯(cuò)啦!<br/>錯(cuò)誤號(hào):".$errors[0]['errno']."<br/>SQL錯(cuò)誤狀態(tài):".$errors[0]['sqlstate']."<br/>錯(cuò)誤信息:".$errors[0]['error']);
die();
}
return $res;
}
/**
* 插入數(shù)據(jù)
*
* @param <type> $data The data
*
* @return boolean ( description_of_the_return_value )
*/
public function insert($data){
$link = $this->_db;
if(!$link)return false;
if(is_array($data)){
$keys = '';
$values = '';
foreach ($data as $key => $value) {
$keys .= "`".$key."`,";
$values .= "'".$value."',";
}
$keys = rtrim($keys,',');
$values = rtrim($values,',');
}
$sql = "INSERT INTO `{$this->_table}`({$keys}) VALUES({$values})";
mysqli_query($this->_db,$sql);
$insertId = mysqli_insert_id($this->_db);
return $insertId;
}
/**
* 更新數(shù)據(jù)
*
* @param <type> $data The data
*
* @return <type> ( description_of_the_return_value )
*/
public function update($data){
$link = $this->_db;
if(!$link)return $link;
if(is_array($data)){
$dataStr = '';
foreach ($data as $key => $value) {
$dataStr .= "`".$key."`='".$value."',";
}
$dataStr = rtrim($dataStr,',');
}
$sql = "UPDATE `{$this->_table}` SET {$dataStr} {$this->_where} {$this->_order} {$this->_limit}";
$res = $this->execute($sql);
return $res;
}
/**
* 刪除數(shù)據(jù)
*
* @return <type> ( description_of_the_return_value )
*/
public function delete(){
$link = $this->_db;
if(!$link)return $link;
$sql = "DELETE FROM `{$this->_table}` {$this->_where}";
$res = $this->execute($sql);
return $res;
}
/**
* 異常信息輸出
*
* @param <type> $var The variable
*/
private function ShowException($var){
if(is_bool($var)){
var_dump($var);
}else if(is_null($var)){
var_dump(NULL);
}else{
echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>".print_r($var,true)."</pre>";
}
}
}
$db = new Db();
//查詢(xún)操作
var_dump($db->table('user')->where('id > 2')->order('id desc')->limit('2,4')->select());
//插入操作
var_dump($db->table('user')->insert(array('username'=>'user','password'=>'pwd')));
//更新操作
var_dump($db->table('user')->where('id = 1')->update(array('username'=>'user1','password'=>'pwd1')));
//刪除操作
var_dump($db->table('user')->where('id = 1')->delete());
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP PDO預(yù)處理語(yǔ)句及事務(wù)的使用
- PHP的PDO預(yù)處理語(yǔ)句與存儲(chǔ)過(guò)程
- php_pdo 預(yù)處理語(yǔ)句詳解
- PHP5中使用mysqli的prepare操作數(shù)據(jù)庫(kù)的介紹
- php pdo操作數(shù)據(jù)庫(kù)示例
- PHP入門(mén)教程之使用Mysqli操作數(shù)據(jù)庫(kù)的方法(連接,查詢(xún),事務(wù)回滾等)
- PHP使用PDO操作數(shù)據(jù)庫(kù)的亂碼問(wèn)題解決方法
- PHP中使用匿名函數(shù)操作數(shù)據(jù)庫(kù)的例子
- PHP中的MYSQL常用函數(shù)(php下操作數(shù)據(jù)庫(kù)必備)
- php 使用預(yù)處理語(yǔ)句操作數(shù)據(jù)庫(kù)
相關(guān)文章
windows下zendframework項(xiàng)目環(huán)境搭建(通過(guò)命令行配置)
本文將詳細(xì)介紹windows下通過(guò)命令行配置zendframework項(xiàng)目環(huán)境,需要了解的朋友可以參考下2012-12-12
PHP 實(shí)現(xiàn)頁(yè)面靜態(tài)化的幾種方法
這篇文章主要介紹了PHP 實(shí)現(xiàn)頁(yè)面靜態(tài)化的幾種方法,需要的朋友可以參考下2017-07-07
iis6手工創(chuàng)建網(wǎng)站后無(wú)法運(yùn)行php腳本的解決方法
下面小編就為大家?guī)?lái)一篇iis6手工創(chuàng)建網(wǎng)站后無(wú)法運(yùn)行php腳本的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
PHP實(shí)現(xiàn)模擬http請(qǐng)求的方法分析
這篇文章主要介紹了PHP實(shí)現(xiàn)模擬http請(qǐng)求的方法,簡(jiǎn)單分析了http請(qǐng)求的原理、流程及php實(shí)現(xiàn)模擬http請(qǐng)求的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
PHP fastcgi模式上傳大文件(大約有300多K)報(bào)錯(cuò)
上傳圖片時(shí),大約有300多K,結(jié)果報(bào)了個(gè)服務(wù)器錯(cuò)誤,fastcgi默認(rèn)的請(qǐng)求大小為131072,于是在apache配置中添加了MaxRequestLen 配置就好了2014-09-09
php中簡(jiǎn)單的對(duì)稱(chēng)加密算法實(shí)現(xiàn)
最近突發(fā)奇想要往數(shù)據(jù)庫(kù)里保存一些機(jī)密的東西,然后就想著怎么讓別人即使進(jìn)入到了數(shù)據(jù)庫(kù)也看不懂存儲(chǔ)的是什么,那么只有加密了;可是我們自己還要看呢,那只能找一些對(duì)稱(chēng)加密的算法了,我們想看的時(shí)候再解密回來(lái)。下面就介紹了php中簡(jiǎn)單的對(duì)稱(chēng)加密算法實(shí)現(xiàn)。2017-01-01

