Php design pattern factory pattern _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Php design mode: Factory mode. I often use mysql databases, so the program only writes mysql database operations. I hope you can write all the other classes in your hand. you 'd better send me one. Db_mysql.php inherits the db. php interface and is commonly used in mysql databases. Therefore, the program only writes the mysql database operation class. I hope you can write all the other classes in your hand. you 'd better send me one.
Db_mysql.php inherits the db. php interface and implements various database operations methods. if you are sure that your database platform will not change, you don't need a factory class. just use this.

The code is as follows:


/**
* @ 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 setting when mysql version is 4.1 or later
If ($ this-> version ()> '4. 1' & $ dbcharset)
{
Mysql_query ("set names '". $ dbcharset. "'", $ this-> connid );
}
// Set the SQL mode when mysql version is 5.0 or later.
If ($ this-> version ()> '5. 0 ')
{
Mysql_query ("SET SQL _mode ='' ", $ this-> connid );
}
If (! Emptyempty ($ dbname )){
$ This-> select_db ($ dbname );
}

}
/**
* Select a database
*
* @ Param unknown_type $ dbname
*/
Public function select_db ($ dbname ){
Mysql_select_db ($ dbname, $ this-> connid );

}
/**
* Execute SQL statements
*
* @ Param unknown_type $ sqlstr
*/
Public function query ($ sqlstr ){
$ Query = mysql_query ($ sqlstr, $ this-> connid );
If (! $ Query ){
$ This-> halt ('MySQL Query error', $ sqlstr );
}
Return $ query;
}
/**
* Retrieve 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 associated 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 );
}

/**
* Obtain the number of rows affected by the previous MySQL operation.
* @ Return int
*/
Public function affected_rows ()
{
Return mysql_affected_rows ($ this-> connid );
}
/**
* Number of rows in the result set
* @ Return int
*/
Public function num_rows ($ query)
{
Return mysql_num_rows ($ query );
}

/**
* Number of fields in the returned result set
* @ Return int
*/
Public function num_fields ($ query)
{
Return mysql_num_fields ($ query );
}

/**
* Release the result memory.
*
* @ Param unknown_type $ query
* @ Return bool
*/
Public function free_result ($ query)
{
Return mysql_free_result ($ query );
}
/**
* Obtain the ID generated by the previous INSERT operation.
* @ Return int
*/
Public function insert_id ()
{
Return mysql_insert_id ($ this-> connid );
}

/**
* Obtain MySQL server information
*
* @ Return string
*/
Public function version ()
{
Return mysql_get_server_info ($ this-> connid );
}
/**
* Close the MYSQL connection
*
* @ Return bool
*/
Public function close ()
{
Return mysql_close ($ this-> connid );
}
/**
* Returns an error string.
*
* @ Return string
*/Private function error (){
Return @ mysql_error ($ this-> connid );
}
/**
* The error code is returned.
*
* @ Return int
*/
Private function errno (){
Return intval (@ mysql_errno ($ this-> connid ));
}
/**
* Output error information
*
* @ 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, which defines database operation methods.

The code is as follows:


Interface db {

Function select_db ($ dbname); // select a database
Function query ($ sqlstr); // execute an SQL statement
Function get_one ($ sqlstr); // execute an SQL statement to obtain only one record
Function fetch_array ($ query); // retrieves a row from the result set as an associated array
Function affected_rows (); // number of records affected by the return operation
Function num_rows ($ query); // Obtain the number of rows in the result set.
Function num_fields ($ query); // number of fields in the returned 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 the database connection
}
?>


Db_factory.php database factory class, which must be used more conveniently to implement the database platform

The code is as follows:


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

Bytes. I hope you can write all the other classes in your hand. you 'd better send me one. Db_mysql.php inherits the db. php interface,...

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.