php 设计模式之 工厂模式

php 数据查询与连接类

本身常用mysql数据库,所以程序只写了mysql的数据库操作类。希望各位高手把别的的类写全,最好能发一份给自家。
db_mysql.php世袭db.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 {

mysql教程i 数据连接类
class Mydb extends mysqli
{
    protected $_databasehost = ‘localhost’;
    protected $_databaseuser = ‘root’;
    protected $_databasepass = ‘123456’;
    protected $_databasename = ‘test’;
    protected $_databaseport = ‘3306’;
 protected $_fetch_method = MYSQLI_ASSOC;
 protected $_databasechar = ‘utf8’;
   
 /**
  * Enter description here…
  *
  * @param string $host
  * @param string $user
  * @param string $pass
  * @param string $name
  * @param int $port
  * @param string $char
  */
    function
__construct($host=”,$user=”,$pass=”,$name=”,$port=”,$char=”)
    {   
        $this->_databasehost = empty($host) ?
$this->_databasehost : $host;
        $this->_databaseuser = empty($user) ?
$this->_databaseuser : $user;
        $this->_databasepass = empty($pass) ?
$this->_databasepass : $pass;
        $this->_databasename = empty($name) ?
$this->_databasename : $name;
        $this->_databaseport = empty($port) ?
$this->_databaseport : $port;
  $this->_databasechar = empty($char) ? $this->_databasechar :
$char;
        try{
           
parent::__construct($this->_databasehost,$this->_databaseuser,
                $this->_databasepass,$this->_databasename,
                $this->_databaseport);
            if(mysqli_connect_errno()){
                die(“服务器连接战败”State of Qatar;
            }
            if(!parent::set_charset($this->_databasechar)){
                die(“数据库教程不能够利用utf-8编码”卡塔尔;
            }           
        }catch(Exception $e){
            die($e->getMessage());
        }   
    }
   

?php/*** Class program for yinghua05-2* designer :songsong*/

private $connid;

 function selectDb($name)
 {
   $this->_databasename = empty($name) ? $this->_databasename :
$name;
   return $this->select_db($this->_databasename);
 }

class MySQL {var $link;var $result;var $querys;function MySQL($host =
”, $user = ”, $pw = ”, $db = ”, $encode = ‘UTF8’) {$this-querys =
0;if($host != ” $user != ” $db != ”)
{$this-connect($host,$user,$pw,$db,$encode);}}/** * connect to
database * * @param unknown_type $host * @param unknown_type $user
* @param unknown_type $pw * @param unknown_type $db * @return
boolean */function connect($host,$user,$pw,$db,$encode = ‘UTF8’)
{$resource = mysql_connect($host,$user,$pw);if(is_resource($resource))
{$this-link = $resource;if(mysql_select_db($db,$this-link))
{unset($resource);if (floatval(mysql_get_server_info($this-link)) 4.1
isset($encode)) {mysql_query(‘SET NAMES ‘.$encode.”);}return true;}
else {unset($resource);return false;}} else {unset($resource);return
false;}}/** * query sql * * @param unknown_type $query * @return
unknown */function query($query) {$result =
mysql_query($query,$this-link);$this-querys ++;if($result)
{$this-result = $result;return true;} else {return false;}}/** *
fetch a row * * @return mixed */function fetch()
{if(is_resource($this-result)) {return
mysql_fetch_array($this-result);} else {return false;}}/** * fetch
all result * * @return mixed */function fetchAll()
{if(is_澳门新葡萄京官网首页,resource($this-result)) {$temp = array();while ($row =
mysql_fetch_array($this-result)) {$temp[] = $row;}return $temp;}
else {return false;}}/** * return the querys * * @return integer
*/function getQuerys() {return $this-querys;}/** * get the numbers
of the result * * @return int */function getNumberRow() {if
(is_resource($this-result)) {return mysql_num_rows($this-result);}
else {return 0;}}/** * free result * * @return boolean */function
free() {if(is_resource($this-result))
{mysql_free_result($this-result);return true;} else {return
false;}}/** * close mysql connect * * @return boolean */function
close() {if(is_resource($this-link)) {mysql_close($this-link);return
true;} else {return false;}}}

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′;
}
// 当mysql版本为4.1之上时,启用数据库字符集设置
if($this->version() > ‘4.1’ && $dbcharset)
{
mysql_query(“SET NAMES ‘”.$dbcharset.”‘” , $this->connid);
}
// 当mysql版本为5.0以上时,设置sql mode
if($this->version() > ‘5.0’)
{
mysql_query(“SET sql_mode=”” , $this->connid);
}
if (!emptyempty($dbname)){
$this->select_db($dbname);
}

    /**
     * Enter description here…
     *
     */
 function __desstruct()
 {
  $this->close();
 }

?

}
/**
* 接纳数据库
*
* @param unknown_type $dbname
*/
public function select_db($dbname){
mysql_select_db($dbname,$this->connid);

 /**
  * 设置再次回到值类型
  *
  * @param int $type
  */
 function setFetchMethod($type=2)
 {
  switch($type)
  {
   case ‘1’:
    $this->_fetch_method = MYSQLI_NUM;
    break;
   case ‘2’:
    $this->_fetch_method = MYSQLI_ASSOC;
    break;
   case ‘3’:
    $this->_fetch_method = MYSQLI_BOTH;
    break;
   default:
    $this->_fetch_method = MYSQLI_ASSOC;
    break;
  }
 }

}
/**
* 执行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 ;
}
/**
* 从结果聚焦获得一行作为关周全组
* @param resource 数据库查询结果财富
* @param string 定义再次来到类型
* @return array
*/
public function fetch_array($query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}

 /**
  * 插入前转义
  *
  * @param string|array $str
  * @return string
  */
 function quoteInto($str)
 {
  if(is_array($str)){
   foreach ($str as $key=>$val){
    $str[$key] = $this->real_escape_string($val);
   }
  }else{
   $str = $this->real_escape_string($str);
  }
  return $str;
 }

/**
* 得到前一回 MySQL 操作所影响的记录行数
* @return int
*/
public function affected_rows()
{
return mysql_affected_rows($this->connid);
}
/**
* 获得结果集中央银行的数量
* @return int
*/
public function num_rows($query)
{
return mysql_num_rows($query);
}

 /**
  * 插入
  *
  * @param string $table
  * @param array $fields
  * @return int
  * @deprecated 键名叫插入的字段值 键值为插入的值
  */
 function insert($table,$fields)
 {
  if(empty($table)){return false;}
  if(empty($fields)){return false;}
  $insertFields = @implode(‘,’,@array_keys($fields));
  $arrayValues = @array_values($fields);
  $result = $this->quoteInto($arrayValues);
  $insertValues =
“‘”[email protected](“‘,'”,$result).”‘”;
  $sql = “INSERT INTO “.$table.”(“.$insertFields.”)
    VALUES(“.$insertValues.”)”;
  $this->query($sql);
  return $this->insert_id;
 }
 

/**
* 重返结果聚集字段的多寡
* @return int
*/
public function num_fields($query)
{
return mysql_num_fields($query);
}

 /**
  * 更新
  *
  * @param string $table
  * @param array $fields
  * @param string $where
  * @return null
  * @deprecated 键名字为插入的字段值 键值为插入的值
  */
 function update($table,$fields,$where)
 {
  if(empty($table)){return false;}
  if(empty($fields)){return false;}
  if(empty($where)){return false;}
  foreach($fields as $key=>$v)
  {
   $condition[] = “{$key}='”.$this->quoteInto($v).”‘”;
  }
  $sql = “UPDATE “.$table.”
    SET “.implode(‘,’,$condition).” WHERE “.$where;
  $this->query($sql);
 }

/**
* 释放结果内部存款和储蓄器
*
* @param unknown_type $query
* @return bool
*/
public function free_result($query)
{
return mysql_free_result($query);
}
/**
* 得到上一步 INSERT 操作产生的 ID
* @return int
*/
public function insert_id()
{
return mysql_insert_id($this->connid);
}

 /**
  * 获取一条数据
  *
  * @param string $sql
  * @return array 一维数组
  */
 function fetchRow($sql)
 {
  $result = $this->query($sql);
  $row = $result->fetch_array($this->_fetch_method);
  return $row;
 }

/**
* 得到 MySQL 服务器新闻
*
* @return string
*/
public function version()
{
return mysql_get_server_info($this->connid);
}
/**
* 关闭MYSQL连接
*
* @return bool
*/
public function close()
{
return mysql_close($this->connid);
}
/**
* 重返错误字符串
*
* @return string
*/ private function error(){
return @mysql_error($this->connid);
}
/**
* 重返错误号
*
* @return int
*/
private function errno(){
return intval(@mysql_errno($this->connid)) ;
}
/**
* 输出出错音讯
*
* @param string $message
* @param string $sql
*/
private function halt($message = ”, $sql = ”){
exit(“MySQL Query:$sql
MySQL Error:”.$this->error().”
MySQL Errno:”.$this->errno().”
Message:$message”);
}
}
?>

 /**
  * 获取全体数码
  *
  * @param string $sql
  * @return array 二维数组
  */
 function fetchAll($sql)
 {
  $result = $this->query($sql);
  while($row = $result->fetch_array($this->_fetch_method))
  {
   $data[] = $row;
  }
  return $data;
 }

db.php数据库操作接口,定义数据库操作的方法. 复制代码 代码如下:
interface db {

 function fetchFree()
 {
  return $this->free_result();
 }

function select_db($dbname卡塔尔(قطر‎;//采纳数据库
function query($sqlstr);//执行sql语句
function get_one($sqlstrState of Qatar;//实践sql语句,只获得一条记下
function fetch_array($queryState of Qatar;//从结果集中获得一行作为涉及数组
function affected_rows(卡塔尔国;//重回操作所影响的笔录行数
function num_rows($queryState of Qatar;//取得结果聚集央银行的多寡
function num_田野s($query卡塔尔(قطر‎;//再次回到结果聚焦字段的数目
function free_result($queryState of Qatar;//释放财富
function insert_id(卡塔尔(قطر‎;//重临上一遍插入记录的ID;
function version(卡塔尔国;//数据库版本
function close(卡塔尔;//关闭数据库连接
}
?>

 /**
  * Enter description here…
  *
  * @param string $type
  * @return unknown
  */
 function getVersion($type=’server’)
 {
  switch($type){
   case ‘server’:
    $result = $this->server_version;
    break;
   case ‘client’:
    $result = $this->client_version;
    break;
   default:
    $result = $this->server_version;
    break;
  }
  return $result;
 }

db_factory.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() {
}
}
?>

 /**
  * Enter description here…
  *
  * @param string $type
  * @return unknown
  */
 function getInfo($type=’server’)
 {
  switch($type){
   case ‘server’:
    $result = $this->server_info;
    break;
   case ‘client’:
    $result = $this->client_info;
    break;
   default:
    $result = $this->server_info;
    break;
  }
  return $result;
 }
}

数据连接类 class Mydb extends mysqli {
protected $_databasehost = ‘localhost’; protected $_databaseuser =
‘root’; protected $_databasepass = ‘123456’; protected $…

发表评论

电子邮件地址不会被公开。 必填项已用*标注