PHP Singleton mode package MySQL class

Source: Internet
Author: User

Class Mysqlconn {
Defines a private static property that is used to hold an instantiated object
private static $dbcon;
Defines a private static property that is used to connect to a database
private static $conn;
Define a private destructor to connect to the database
Private Function __construct ()
Self:: $conn = Mysqli_connect ($host, $username, $password, $dbname) or Die (' link failed: '. Mysqli_error (). Mysqli_errno ());
To define a private clone method
Private Function __clone ()
Todo:implement __clone () method.
Common static method used to return class instances
public static function getinstance () {
if (!self:: $dbcon) {
Self:: $dbcon = new self ();
Return self:: $dbcon;
The various functions of MySQL can be defined later
Public Function Search ($sql) {
$result = Mysqli_query (self:: $conn, $sql);
Return Mysqli_fetch_assoc ($result);
Calling methods
$conn = Mysqlconn::getinstance ();
$sql = "SELECT * FROM ' table_name '";
$rows = $conn->search ($sql);
Echo ' <pre> ';
Var_dump ($rows);

PHP Singleton mode package MySQL class

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: 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.