search
HomePHP LibrariesOther librariesSingleton mode implements PHP class of mysql
Singleton mode implements PHP class of mysql
<?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.

Disclaimer

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

Simple implementation method of php singleton mode, php instance modeSimple implementation method of php singleton mode, php instance mode

06Jul2016

A simple implementation method of PHP singleton mode, PHP instance mode. Simple implementation method of php singleton mode, php example mode Simple implementation method of php singleton mode php /** * Singleton mode of design mode * $_instance must be declared as a static private variable * Construction

Simple implementation method of php singleton mode, php instance mode_PHP tutorialSimple implementation method of php singleton mode, php instance mode_PHP tutorial

12Jul2016

A simple implementation method of PHP singleton mode, PHP instance mode. Simple implementation method of php singleton mode, php example mode Simple implementation method of php singleton mode php /** * Singleton mode of design mode * $_instance must be declared as a static private variable * Construction

How Do I Link Static Libraries That Depend on Other Static Libraries?How Do I Link Static Libraries That Depend on Other Static Libraries?

13Dec2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

PHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base_PHP tutorialPHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base_PHP tutorial

12Jul2016

PHP implements a complete instance of the Model base class based on mysqli, phpmysqlimodel base. PHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base This article describes an example of PHP implementing the Model base class based on mysqli. Share it with everyone for your reference, the details are as follows: DB.c

PHP implementation of generating a complete instance of the MYSQL statement class through parameters, mysql statement_PHP tutorialPHP implementation of generating a complete instance of the MYSQL statement class through parameters, mysql statement_PHP tutorial

12Jul2016

PHP implements the generation of complete instances of MYSQL statement classes and mysql statements through parameters. A complete example of generating a MYSQL statement class through parameters in PHP, mysql statement. This example describes how to generate a MYSQL statement class through parameters in PHP. Share it with everyone for your reference, the details are as follows

How to Silence TensorFlow\'s Debugging Output?How to Silence TensorFlow\'s Debugging Output?

28Oct2024

Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...

See all articles