<?php
/*
mysql数据库操作类
0、实现了单例模式
1、该类一实例化,就可以自动连接上mysql数据库
2、该类可以单独去设定要使用的连接编码
3、该类可以单独设定要使用的数据库
4、可以主动关闭数据库连接
5、完成了DML、DQL操作的简单封装
6、实现了简单的sql执行错误处理
*/
class MySQLDB{
private $link = null; #用于存储连接数据库成功后的“资源”
protected $host;
protected $port;
protected $user;
protected $password;
protected $charset;
protected $dbname;
/**
单例实现
//*/
private static $instance = null; #用于存储该类的唯一实例
private function __clone(){} #禁止该类的实例对象进行克隆复制对象
#对外提供一个创建该类实例的方法
public static function getInstance($config){
if(!(static::$instance instanceof self)){
static::$instance = new self($config);
}
return static::$instance;
}
#实现单例的基础:私有化该类的构造方法
private function __construct($config){
$this->host = isset($config['host'])?$config['host']:'localhost';
$this->port = isset($config['port'])?$config['port']:'3306';
$this->user = isset($config['user'])?$config['user']:'root';
$this->password = isset($config['password'])?$config['password']:'root';
$this->charset = isset($config['charset'])?$config['charset']:'utf8';
$this->dbname = isset($config['dbname'])?$config['dbname']:'db_mvc';
$this->link = @mysql_connect(
"{$this->host}:{$thi->port}","{$this->user}","$this->password")
or die(连接失败!);
$this->selectDB($this->dbname);
$this->setCharset($this->charset);
}
public function setCharset($charset){#1设置连接环境字符编码
$sql = "set names {$charset}";
$this->query($sql);
}
public function selectDB($dbname){#2选择要操作的数据库
$sql = "use {$dbname}";
$this->query($sql);
}
public function closeDB(){#3关闭数据库连接
if(isset($this->link)){
mysql_query($this->link);
}
}
public function execute($sql){#增删改
$this->query($sql);
return true;
}
public function getData($sql){#返回结果是一个标量值
$result = $this->query($sql);
$num = mysql_fetch_array($result);
mysql_free_result($result);
return $num[0];
}
public function getRow($sql){#返回结果是一个一维数组
$result = $this->query($sql);
$row = mysql_fetch_array($result);
mysql_free_result($result);
return $row;
}
public function getRows($sql){#返回结果是一个二维数组
$result = $this->query($sql);
$arr = array();
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}
mysql_free_result($result);
return $arr;
}
private function query($sql){#错误处理并返回一个结果集
$result = mysql_query($sql);
if($result === false){
echo "代码执行错误!请参考如下提示:";
echo "<br />错误代号:".mysql_errno();
echo "<br />错误内容:".mysql_error();
echo "<br />错误代码:".$sql;
die();
}
return $result;
}
}
?>
PHP MySQL工具类的简单封装
最新推荐文章于 2024-08-17 23:25:50 发布