欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 設(shè)計(jì)模式之 工廠模式

 更新時(shí)間:2008年12月19日 12:51:38   作者:  
用工廠模式設(shè)計(jì)數(shù)據(jù)庫操作類,本程序要運(yùn)行在PHP5.0以上,使用本程序可以方便的更換數(shù)據(jù)庫,并且可擴(kuò)展到其它數(shù)據(jù)庫平臺(tái);在換數(shù)據(jù)庫平臺(tái)時(shí),只是簡單的修改數(shù)據(jù)庫類型就可以了。
本人常用mysql數(shù)據(jù)庫,所以程序只寫了mysql的數(shù)據(jù)庫操作類。希望各位高手把另外的類寫全,最好能發(fā)一份給我。
db_mysql.php繼承db.php接口,具體實(shí)現(xiàn)數(shù)據(jù)庫操作的各種方法 ,如果你確定你的數(shù)據(jù)庫平臺(tái)不會(huì)變的話不用工廠類,直接用這個(gè)就行了。
復(fù)制代碼 代碼如下:

<?php
/**
* @author 黃建文
* @version V1.0
* @email hjwtp2005@qq.com
* @data 2008-12-16
* ==================================================================
* @example
* include 'db_mysql.php';
* $db=new db_mysql('localhost','root','admin','emtit');
* $sqlstr="SELECT * FROM member WHERE memberid=1";
* var_dump($db->get_one($sqlstr));
* ===================================================================
*/
include 'db.php';
class db_mysql implements db {

private $connid;

public function db_mysql($dbhost,$username,$passowrd,$dbname='',$dbcharset='utf8'){
$this->connid=mysql_connect($dbhost,$username,$passowrd);
if (!$this->connid){
$this->halt('Can not connect to MySQL server');
}
if (emptyempty($dbcharset)){
$dbcharset='utf8';
}
// 當(dāng)mysql版本為4.1以上時(shí),啟用數(shù)據(jù)庫字符集設(shè)置
if($this->version() > '4.1' && $dbcharset)
{
mysql_query("SET NAMES '".$dbcharset."'" , $this->connid);
}
// 當(dāng)mysql版本為5.0以上時(shí),設(shè)置sql mode
if($this->version() > '5.0')
{
mysql_query("SET sql_mode=''" , $this->connid);
}
if (!emptyempty($dbname)){
$this->select_db($dbname);
}

}
/**
* 選擇數(shù)據(jù)庫
*
* @param unknown_type $dbname
*/
public function select_db($dbname){
mysql_select_db($dbname,$this->connid);

}
/**
* 執(zhí)行SQL語句
*
* @param unknown_type $sqlstr
*/
public function query($sqlstr){
$query=mysql_query($sqlstr,$this->connid);
if (!$query){
$this->halt('MySQL Query Error', $sqlstr);
}
return $query;
}
/**
* 取得一條查詢記錄
*
* @return unknown
*/
public function get_one($sqlstr){
$query=$this->query($sqlstr);
$rs = $this->fetch_array($query);
$this->free_result($query);
return $rs ;
}
/**
* 從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組
* @param resource 數(shù)據(jù)庫查詢結(jié)果資源
* @param string 定義返回類型
* @return array
*/
public function fetch_array($query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}

/**
* 取得前一次 MySQL 操作所影響的記錄行數(shù)
* @return int
*/
public function affected_rows()
{
return mysql_affected_rows($this->connid);
}
/**
* 取得結(jié)果集中行的數(shù)目
* @return int
*/
public function num_rows($query)
{
return mysql_num_rows($query);
}

/**
* 返回結(jié)果集中字段的數(shù)目
* @return int
*/
public function num_fields($query)
{
return mysql_num_fields($query);
}

/**
* 釋放結(jié)果內(nèi)存
*
* @param unknown_type $query
* @return bool
*/
public function free_result($query)
{
return mysql_free_result($query);
}
/**
* 取得上一步 INSERT 操作產(chǎn)生的 ID
* @return int
*/
public function insert_id()
{
return mysql_insert_id($this->connid);
}

/**
* 取得 MySQL 服務(wù)器信息
*
* @return string
*/
public function version()
{
return mysql_get_server_info($this->connid);
}
/**
* 關(guān)閉MYSQL連接
*
* @return bool
*/
public function close()
{
return mysql_close($this->connid);
}
/**
* 返回錯(cuò)誤字符串
*
* @return string
*/ private function error(){
return @mysql_error($this->connid);
}
/**
* 返回錯(cuò)誤號(hào)
*
* @return int
*/
private function errno(){
return intval(@mysql_errno($this->connid)) ;
}
/**
* 輸出出錯(cuò)信息
*
* @param string $message
* @param string $sql
*/
private function halt($message = '', $sql = ''){
exit("MySQL Query:$sql <br> MySQL Error:".$this->error()." <br> MySQL Errno:".$this->errno()." <br> Message:$message");
}
}
?>

db.php數(shù)據(jù)庫操作接口,定義數(shù)據(jù)庫操作的方法.
復(fù)制代碼 代碼如下:

<?php
interface db {

function select_db($dbname);//選擇數(shù)據(jù)庫
function query($sqlstr);//執(zhí)行sql語句
function get_one($sqlstr);//執(zhí)行sql語句,只得到一條記錄
function fetch_array($query);//從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組
function affected_rows();//返回操作所影響的記錄行數(shù)
function num_rows($query);//取得結(jié)果集中行的數(shù)目
function num_fields($query);//返回結(jié)果集中字段的數(shù)目
function free_result($query);//釋放資源
function insert_id();//返回上一次插入記錄的ID;
function version();//數(shù)據(jù)庫版本
function close();//關(guān)閉數(shù)據(jù)庫連接
}
?>

db_factory.php數(shù)據(jù)庫工廠類,要實(shí)現(xiàn)數(shù)據(jù)庫平臺(tái)更方便一定要使用這個(gè)類
復(fù)制代碼 代碼如下:

<?php
/**
* @author 黃建文
* @version v1.0
* @email hjwtp2005@qq.com
* @example
* $db=db_factcory::create('MYSQL','localhost','root','admin','emtit');
* $sqlstr="SELECT * FROM member WHERE memberid=1";
* $db->get_one($sqlstr);
*/
include 'db_mysql.php';
class db_factory {
function db_factory() {
}
static function create($type,$dbhost,$username,$password,$dbname='',$dbcharset=''){
switch ($type){
case 'MYSQL':
return new db_mysql($dbhost,$username,$password,$dbname,$dbcharset);
case 'SQLSERVER':
return new db_sqlserver($dbhost,$username,$password,$dbname,$dbcharset);
case 'ACCESS':
return new db_access($dbhost,$username,$password,$dbname,$dbcharset);
case 'ORACLE':
return new db_oracle($dbhost,$username,$password,$dbname,$dbcharset);
}
return false;
}
function __destruct() {
}
}
?>

相關(guān)文章

  • Discuz開啟Gzip壓縮多種方法匯總

    Discuz開啟Gzip壓縮多種方法匯總

    GZip可對(duì)多種類型的文件進(jìn)行壓縮,對(duì)于CSS、JS、HTML文件具有極高的壓縮率,尤其對(duì)使用了較多JS特效的網(wǎng)站,開啟GZip后壓縮比高達(dá)70%。
    2023-05-05
  • PHP中Memcache操作類及用法實(shí)例

    PHP中Memcache操作類及用法實(shí)例

    這篇文章主要介紹了PHP中Memcache操作類及用法,以實(shí)例形式詳細(xì)分析了Memcache類連接數(shù)據(jù)庫及進(jìn)行緩存操作的具體用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解

    Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解

    這篇文章主要介紹了Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解,需要的朋友可以參考下
    2020-04-04
  • php中str_replace替換實(shí)例講解

    php中str_replace替換實(shí)例講解

    這篇文章主要介紹了php中str_replace替換實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • PHP函數(shù)http_build_query使用詳解

    PHP函數(shù)http_build_query使用詳解

    這篇文章主要介紹了PHP函數(shù)http_build_query使用詳解,分別對(duì)傳入一維關(guān)聯(lián)數(shù)組、一維索引數(shù)組、二維數(shù)組、傳入對(duì)象等給出示例,需要的朋友可以參考下
    2014-08-08
  • PHP中的float類型使用說明

    PHP中的float類型使用說明

    使用PHP的float類型需要注意的是:PHP的float類型的精度有點(diǎn)問題。如果需要高精度的數(shù)學(xué)計(jì)算,可以使用php提供的專用的數(shù)學(xué)函數(shù) arbitrary precision math functions系列和gmp系列函數(shù)。
    2010-07-07
  • 解析PHP 使用curl提交json格式數(shù)據(jù)

    解析PHP 使用curl提交json格式數(shù)據(jù)

    本篇文章是對(duì)PHP中使用curl提交json格式數(shù)據(jù)的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php addslashes和mysql_real_escape_string

    php addslashes和mysql_real_escape_string

    本文介紹的是用 mysql_real_escape_string對(duì)用戶提交數(shù)據(jù)進(jìn)行整理處理和通過addslashes以及mysql_escape_string這3個(gè)類似的功能函數(shù)的區(qū)別。經(jīng)過轉(zhuǎn)義的數(shù)據(jù)可以直接插入到數(shù)據(jù)庫中。
    2010-01-01
  • PHP實(shí)現(xiàn)的折半查詢算法示例

    PHP實(shí)現(xiàn)的折半查詢算法示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的折半查詢算法,結(jié)合完整實(shí)例形式分析了php使用遞歸與非遞歸實(shí)現(xiàn)折半查詢的算法操作步驟與使用方法,需要的朋友可以參考下
    2017-10-10
  • PHP中單引號(hào)和雙引號(hào)的區(qū)別詳解

    PHP中單引號(hào)和雙引號(hào)的區(qū)別詳解

    看好多代碼有時(shí)候用單引號(hào)或雙引號(hào)實(shí)現(xiàn)包含字符串的內(nèi)容,其實(shí)簡單個(gè)概括下雙引號(hào)中的變量可以解析,單引號(hào)就是絕對(duì)的字符串,下面這篇文章主要給大家介紹了關(guān)于PHP中單引號(hào)和雙引號(hào)區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-01-01

最新評(píng)論