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>
http://www.bkjia.com/PHPjc/1011351.htmlwww.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...
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