Home  >  Article  >  php教程  >  PHP实现的一个简单的数据库操作类

PHP实现的一个简单的数据库操作类

WBOY
WBOYOriginal
2016-06-13 09:02:411073browse

PHP实现的一个简单的数据库操作类

PHP实现的一个简单的数据库操作类

实现的功能:

- 在实例化的时候能设置连接字符集

- 在实例化的时候能连接数据库

- 在实例化的时候能选择默认数据库

- 销毁对象时关闭数据库

代码如下:

<code class="language-php hljs "><?php
    // 数据库操作类MySQLDB
    class MySQLDB {
        // 声明属性
        private $server;
        private $username;
        private $password;
        public $default_db;
        public $link;
        // 声明构造函数
        public function __construct($server,$username,$password,$default_db) {
            // 设置连接字符串
            $this->server = $server;
            $this->username = $username;
            $this->password = $password;
            $this->default_db = $default_db;
            // 连接数据库
            $this->link = mysql_connect($this->server,$this->username,$this->password);
            if (!$this->link) {
                echo 'database connect failure!';
            }
            // 设置默认的数据库
            $bool = mysql_select_db($this->default_db,$this->link);
            if (!$bool) {
                echo 'Select default_db failure!';
            }
        }
        // 声明析构函数
        public function __destruct() {
            mysql_close($this->link);
        }
    }</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn