提供一款简单实现的连接类是利用php 构造函数自动创建连接与删除操作,有需要的朋友可以参考。
代码如下 复制代码
class mysql {
private $db_host; //数据库主机
private $db_user; //数据库用户名
private $db_pwd; //数据库用户名密码
private $db_database; //数据库名
private $conn; //数据库连接标识;
private $result; //执行query命令的结果资源标识
private $sql; //sql执行语句
private $row; //返回的条目数
private $coding; //数据库编码,gbk,utf8,gb2312
private $bulletin = true; //是否开启错误记录
private $show_error = true; //测试阶段,显示所有错误,具有安全隐患,默认关闭
private $is_error = false; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的
/*构造函数*/
public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_database = $db_database;
$this->conn = $conn;
$this->coding = $coding;
$this->connect();
}
/*数据库连接*/
public function connect() {
if ($this->conn == pconn) {
//永久链接
$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
//即使链接
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
if (!mysql_select_db($this->db_database, $this->conn)) {
if ($this->show_error) {
$this->show_error(数据库不可用:, $this->db_database);
}
}
mysql_query(set names $this->coding);
}
/*数据库执行语句,可执行查询添加修改删除等任何sql语句*/
public function query($sql) {
if ($sql == ) {
$this->show_error(sql语句错误:, sql查询语句为空);
}
$this->sql = $sql;
$result = mysql_query($this->sql, $this->conn);
if (!$result) {
//调试中使用,sql语句出错时会自动打印出来
if ($this->show_error) {
$this->show_error(错误sql语句:, $this->sql);
}
} else {
$this->result = $result;
}
return $this->result;
}
/*创建添加新的数据库*/
public function create_database($database_name) {
$database = $database_name;
$sqldatabase = 'create database ' . $database;
$this->query($sqldatabase);
}
http://www.bkjia.com/phpjc/631326.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631326.htmltecharticle提供一款简单实现的连接类是利用php 构造函数自动创建连接与删除操作,有需要的朋友可以参考。 代码如下 复制代码 class mysql { private $d...