PHP Design Pattern Factory Pattern_PHP Tutorial
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.
/**
* @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.
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
/**
* @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() {
}
}
?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor