Home > Backend Development > PHP Tutorial > A simple database operation class implemented in PHP_PHP tutorial

A simple database operation class implemented in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:51:51
Original
814 people have browsed it

A simple database operation class implemented by PHP

A simple database operation class implemented in PHP

Function implemented:

- Ability to set the connection character set during instantiation

- Can connect to the database during instantiation

- Ability to select default database during instantiation

- Close the database when destroying objects

The code is as follows:

<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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1011351.htmlTechArticleA simple database operation class implemented by PHP A simple database operation class implemented by PHP Functions implemented: - In The connection character set can be set when instantiating - when instantiating...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template