<?php defined('ACC')||exit('Access Denied'); // 封装mysql操作类,包括连接功能,及查询功能. class mysql extends absdb{ protected static $ins = null; protected $host; // 主机名 protected $user; // 用户名 protected $passwd; // 密码 protected $db; // 数据库名 protected $port; // 端口 protected $conn = null; // 在内部操作,获得一个对象 public static function getIns() { if(self::$ins === null) { self::$ins = new self(); } $conf = conf::getIns(); self::$ins->host = $conf->host; self::$ins->user = $conf->user; self::$ins->passwd = $conf->pwd; self::$ins->db = $conf->db; self::$ins->port = $conf->port; self::$ins->connect(); self::$ins->select_db(); self::$ins->setChar(); return self::$ins; } // 不让外部做new操作, protected function __construct() { } // 连接数据库 public function connect() { $this->conn = @mysql_connect($this->host,$this->user,$this->passwd,$this->port); if(!$this->conn) { $error = new Exception('数据库连不上',9); throw $error; } } // 发送sql查询 public function query($sql) { $rs = mysql_query($sql,$this->conn); if(!$rs) { log::write($sql); } return $rs; }
This is a PHP class that implements mysql in singleton mode. Friends who need it can download it and use it.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
01 Jun 2018
The code of this article was rewritten using a previous class called MyPDO. The singleton mode was introduced to ensure that this class will not be instantiated repeatedly in global calls and reduce the waste of system resources. Friends in need can refer to it, let’s take a look below.
31 May 2018
This article mainly introduces the MySQL class encapsulated by PHP based on the singleton mode, and analyzes the definition and usage of the MySQL class encapsulated by PHP using the singleton mode in the form of a complete example. Friends who need it can refer to it.
29 Jul 2016
:This article mainly introduces the mysql class implemented by PHP based on the singleton mode. Students who are interested in PHP tutorials can refer to it.
13 Jul 2016
PHP implements singleton() singleton mode instance, singleton instance. PHP implements singleton() singleton mode instance, singleton instance This article describes the method of PHP implementing singleton() singleton mode. Share it with everyone for your reference. The specific implementation method is as follows:
08 Jun 2018
A singleton mode Mysql operation class encapsulated by PHP plays an important role in PHP. This article will explain its related knowledge in detail.
21 Dec 2016
This article mainly introduces the MySQL class encapsulated by PHP based on the singleton mode, and analyzes the definition and usage of the MySQL class encapsulated by PHP using the singleton mode in the form of a complete example. Friends who need it can refer to it.
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images