PHP design mode Factory mode _php tutorial

Source: Internet
Author: User
Tags mysql query php database
I commonly use MySQL database, so the program only writes the MySQL database operation class. I hope that you master to write another class of all, the best can send a copy to me.
db_mysql.php inherit the db.php interface, the implementation of various methods of database operations, if you are sure that your database platform will not change without the factory class, directly with this on the line.
Copy CodeThe code is as follows:
/**
* @author Ricky Wong
* @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 the 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 resources
* @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 INSERT operation in the previous step
* @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, define the method of database operation.
Copy CodeThe code is as follows:
Interface DB {

function select_db ($dbname);//Select Database
function query ($sqlstr);//Execute SQL statement
function Get_one ($SQLSTR);//Execute SQL statement, get only one record
function Fetch_array ($query);//Get a row from the result set as an associative array
function affected_rows ();//The number of record rows affected by the return 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);//Release resources
function insert_id ();//returns the ID of the last inserted record;
function version ();//Database version
function close ();//Close database connection
}
?>

db_factory.php database Factory class, it is more convenient to implement the database platform this class must be used
Copy CodeThe code is as follows:
/**
* @author Ricky Wong
* @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 () {
}
}
?>

http://www.bkjia.com/PHPjc/319675.html www.bkjia.com true http://www.bkjia.com/PHPjc/319675.html techarticle i commonly use MySQL database, so the program only writes the MySQL database operation class. I hope that you master to write another class of all, the best can send a copy to me. db_mysql.php inherit db.php interface, with ...

  • Related Article

    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.