


Use singleton pattern to design a PHP database class_PHP tutorial
Recently, I feel that the website’s database is under a lot of pressure, causing the website’s speed to drop significantly. Because a considerable part of the pages are directly connected to the database to read data, this part of the pages is also changed to use the database singleton class to implement. Now the following class is basically used to connect to the database.
<?php class nmdb { private $link; static private $_instance; // 连接数据库 private function __construct($host, $username, $password) { $this->link = mysql_connect($host, $username, $password); $this->query("SET NAMES 'utf8'", $this->link); //echo mysql_errno($this->link) . ": " . mysql_error($link). "n"; //var_dump($this->link); return $this->link; } private function __clone(){} public static function get_class_nmdb($host, $username, $password) { //$connector = new nmdb($host, $username, $password); //return $connector; if( FALSE == (self::$_instance instanceof self) ) { self::$_instance = new self($host, $username, $password); } return self::$_instance; } // 连接数据表 public function select_db($database) { $this->result = mysql_select_db($database); return $this->result; } // 执行SQL语句 public function query($query) { return $this->result = mysql_query($query, $this->link); } // 将结果集保存为数组 public function fetch_array($fetch_array) { return $this->result = mysql_fetch_array($fetch_array, MYSQL_ASSOC); } // 获得记录数目 public function num_rows($query) { return $this->result = mysql_num_rows($query); } // 关闭数据库连接 public function close() { return $this->result = mysql_close($this->link); } } ?>
The usage of this class is as follows:
$connector = nmdb::get_class_nmdb($host, $username, $password); $connector -> select_db($database);
You can also refer to the following classes:
<?php /* * mysql 单例 */ class mysql{ private $host ='localhost'; //数据库主机 private $user = 'root'; //数据库用户名 private $pwd = ''; //数据库用户名密码 private $database = 'imoro_imoro'; //数据库名 private $charset = 'utf8'; //数据库编码,GBK,UTF8,gb2312 private $link; //数据库连接标识; private $rows; //查询获取的多行数组 static $_instance; //存储对象 /** * 构造函数 * 私有 */ private function __construct($pconnect = false) { if (!$pconnect) { $this->link = @ mysql_connect($this->host, $this->user, $this->pwd) or $this->err(); } else { $this->link = @ mysql_pconnect($this->host, $this->user, $this->pwd) or $this->err(); } mysql_select_db($this->database) or $this->err(); $this->query("SET NAMES '{$this->charset}'", $this->link); return $this->link; } /** * 防止被克隆 * */ private function __clone(){} public static function getInstance($pconnect = false){ if(FALSE == (self::$_instance instanceof self)){ self::$_instance = new self($pconnect); } return self::$_instance; } /** * 查询 */ public function query($sql, $link = '') { $this->result = mysql_query($sql, $this->link) or $this->err($sql); return $this->result; } /** * 单行记录 */ public function getRow($sql, $type = MYSQL_ASSOC) { $result = $this->query($sql); return @ mysql_fetch_array($result, $type); } /** * 多行记录 */ public function getRows($sql, $type = MYSQL_ASSOC) { $result = $this->query($sql); while ($row = @ mysql_fetch_array($result, $type)) { $this->rows[] = $row; } return $this->rows; } /** * 错误信息输出 */ protected function err($sql = null) { //这里输出错误信息 echo 'error'; exit(); } } //用例 $db = mysql::getInstance(); $db2 = mysql::getInstance(); $data = $db->getRows('select * from blog'); //print_r($data); //判断两个对象是否相等 if($db === $db2){ echo 'true'; } ?>

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.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool