The factory pattern _php technique of PHP design pattern

Source: Internet
Author: User
I often use MySQL database, so the program only wrote MySQL database operation class. I hope you master the other class to write all, it is best to send a copy to me.
db_mysql.php inherit the db.php interface and implement various methods of database operation, if you are sure your database platform will not change without the factory class, just use this on the line.
Copy Code code as follows:

<?php
/**
* @author Huang Jianwen
* @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 ';
}
Enable database character set settings when MySQL version is above 4.1
if ($this->version () > ' 4.1 ' && $dbcharset)
{
mysql_query ("SET NAMES '") $dbcharset. "'", $this->connid);
}
When MySQL version is more than 5.0, 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;
}
/**
* Obtain 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 one row from the result set as an associative array
* @param resource database query result resources
* @param string definition return type
* @return Array
*/
Public Function Fetch_array ($query, $result _type = MYSQL_ASSOC)
{
Return mysql_fetch_array ($query, $result _type);
}

/**
* Get the number of records that were 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);
}

/**
* Free up the 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 <br> mysql Error:". $this->error (). "<br> MySQL Errno:". $this->errno (). "< Br> message: $message ");
}
}
?>

db.php Database Operation interface, define the method of database operation.
Copy Code code as follows:

<?php
Interface DB {

function select_db ($dbname);//Select Database
function query ($sqlstr);//Execute SQL statement
function Get_one ($SQLSTR);//Execute SQL statement with only one record
function Fetch_array ($query);//Get one row from the result set as an associative array
function affected_rows ()//return number of rows affected by the operation
function Num_rows ($query);//Get the number of rows in the result set
function Num_fields ($query);//Returns the number of fields in the result set
function Free_result ($query);/Free Resources
function insert_id ();//returns the ID of the last inserted record;
function version ();//Database versions
function close ();//Shut Down database connection
}
?>

db_factory.php database Factory class, to realize the database platform is more convenient to use this class
Copy Code code as follows:

<?php
/**
* @author Huang Jianwen
* @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 ' SQL Server ':
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 () {
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.