Home  >  Article  >  Backend Development  >  PHP Design Pattern Factory Pattern_PHP Tutorial

PHP Design Pattern Factory Pattern_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:48:50928browse

I often use mysql database, so the program only writes the database operation class of mysql. I hope that all the experts will write down all the other categories, and it is best to send a copy to me.
db_mysql.php inherits the db.php interface and specifically implements various methods of database operations. If you are sure that your database platform will not change, you do not need a factory class, just use this one.

Copy code The code is as follows:

/**
* @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';
}
// when When the mysql version is 4.1 or above, enable the database character set setting
if($this->version() > '4.1' && $dbcharset)
{
mysql_query("SET NAMES '".$ dbcharset."'" , $this->connid);
}
// When the mysql version is 5.0 or above, set sql mode
if($this->version() > ' 5.0')
{
mysql_query("SET sql_mode=''" , $this->connid);
}
if (!emptyempty($dbname)){
$this ->select_db($dbname);
}

}
/**
* Select database
*
* @param unknown_type $dbname
*/
public function select_db($dbname){
mysql_select_db($dbname, $this->connid);

}
/**
* Execute SQL statement
*
* @param unknown_type $sqlstr
*/
public function query($sqlstr){
$query=mysql_query($sqlstr,$this ->connid);
if (!$query){
$this->halt('MySQL Query Error', $sqlstr);
}
return $query;
}
/**
* Get a query record
*
* @return unknown
*/
public function get_one($sqlstr){
$query=$this->query($sqlstr);
$rs = $this-> ;fetch_array($query);
$this->free_result($query);
return $rs ;
}
/**
* Get a row from the result set as an associative array
* @param resource database query result resource
* @param string defines the return type
* @return array
*/
public function fetch_array( $query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}

/**
* Get the number of record rows affected by the previous MySQL operation
* @return int
*/
public function affected_rows( )
{
return mysql_affected_rows($this->connid);
}
/**
* Get the number of rows in the result set
* @return int
*/
public function num_rows($query)
{
return mysql_num_rows($query);
}

/**
* Returns the number of fields in the result set
* @return int
*/
public function num_fields($query)
{
return mysql_num_fields($query);
}

/**
* Release result memory
*
* @param unknown_type $query
* @return bool
*/
public function free_result($query)
{
return mysql_free_result($query);
}
/ **
* Get the ID generated by the previous INSERT operation
* @return int
*/
public function insert_id()
{
return mysql_insert_id($this->connid);
}

/**
* Get MySQL server information
*
* @return string
* /
public function version()
{
return mysql_get_server_info($this->connid);
}
/**
* Close MYSQL connection
*
* @return bool
*/
public function close()
{
return mysql_close($this->connid);
}
/**
* Return error string
*
* @return string
*/ private function error(){
return @mysql_error($this-> ;connid);
}
/**
* Return error number
*
* @return int
*/
private function errno(){
return intval(@mysql_errno($this->connid));
}
/**
* Output error message
*
* @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");
}
}
?>

db.php database operation interface, defines the method of database operation.
Copy code The code is as follows:

interface db {

function select_db($dbname);//Select database
function query($sqlstr);//Execute sql statement
function get_one($sqlstr);//Execute the sql statement and only get one record
function fetch_array($query);//Get a row from the result set as an associative array
function affected_rows();//Return operation Number of record rows affected
function num_rows($query);//Get the number of rows in the result set
function num_fields($query);//Return the number of fields in the result set
function free_result($query );//Release resources
function insert_id();//Return the ID of the last inserted record;
function version();//Database version
function close();//Close the database connection
}
?>

db_factory.php database factory class. To make the database platform more convenient, you must use this class
Copy code The code is as follows:

/**
* @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() {
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319675.htmlTechArticleI often use mysql database, so the program only writes the database operation class of mysql. I hope that all the experts will write down all the other categories, and it is best to send a copy to me. db_mysql.php inherits the db.php interface, with...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn