使用单例模式封装mysql的基本操作
需求:
- 实现单例模式
- mysql的基本操作,添加、修改、删除、查询
- 查询多个结果集(二维数组)
- 查询单个结果集 (一维数组)
- 查询单个数据
<?php
class MySQLDB{
public $host;
public $port;
public $username;
public $password;
public $charset;
public $dbname;
//连接结果
private static $link;
private $resourc;
public static function getInstance($config){
if(!isset(self::$link)){
self::$link = new self($config);
}
return self::$link;
}
//构造函数
private function __construct($config){
//初始化数据
$this->host = isset($config['host']) ? $config['host'] : 'localhost';
$this->port = isset($config['port']) ? $config['port'] : '3306';
$this->username = isset($config['username']) ? $config['username'] : 'root';
$this->password = isset($config['password']) ? $config['password'] : '';
$this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
$this->dbname = isset($config['dbname']) ? $config['dbname'] : '';
//连接数据库
$this->connect();
//设定连接编码
$this->setCharset($this->charset);
//选定数据库
$this->selectDb($this->dbname);
}
//禁止克隆
private function __clone(){
}
//进行序列化的时候会调用
public function __sleep(){
echo "序列化。。。";
mysql_close($this->resourc);//关闭连接资源(清理资源)
//如果定义了该__sleep方法,则此时必须返回数组,才能进行序列化
return array('host','port','username','password','charset','dbname');
}
//进行反序列化的时候会调用
public function __wakeup(){
//连接数据库
$this->connect();
//设定连接编码
$this->setCharset($this->charset);
//选定数据库
$this->selectDb($this->dbname);
}
//连接数据库
public function connect(){
$this->resourc = mysql_connect("$this->host:$this->port", "$this->username","$this->password") or die("连接数据库失败!");
}
// 设置字符集
public function setCharset($charset){
//mysql_set_charset($charset, $this->resourc);
$this->query("set names $charset");
}
// 选择数据库
public function selectDb($dbname){
//mysql_select_db($dbname, $this->resourc);
$this->query("use $dbname;") ;
}
/**
* 功能:执行最基本的sql语句
* 返回:如果失败直接结束,如果成功,返回执行结果
*/
public function query($sql){
if(!$result = mysql_query($sql, $this->resourc))
{
echo ("<br />执行失败。");
echo "<br />失败的sql语句为:" . $sql;
echo "<br />出错信息为:" . mysql_error();
echo "<br />错误代号为:" . mysql_errno();
die();
}
return $result;
}
/**
* 功能:执行select语句,返回2维数组
* 参数:$sql 字符串类型 select语句
*/
public function getAll($sql){
$result = $this->query($sql);
$arr = array(); //空数组
while( $rec = mysql_fetch_assoc( $result )){
$arr[] = $rec;//这样就形成二维数组
}
return $arr;
}
//返回一行数据
public function getRow($sql){
$result = $this->query($sql);
//$rec = array();
if( $rec2 = mysql_fetch_assoc( $result )){//返回下标为字段名的数组如果fetch出来有数据(也就是取得了一行数据),结果自然是数组
return $rec2;
}
return false;
}
//返回一个数据
public function getOne($sql){
$result = $this->query($sql);
$rec = mysql_fetch_row($result);//返回下标为数字的数组,且下标一定是0,1,2, 3.....如果没有数据,返回false
if($rec === false){
return false;
}
return $rec[0]; //该数组的第一项。
}
// 插入数据到数据库
public function insert($sql) {
$result = $this->query($sql);
if($result){
return true;
}else{
return false;
}
}
// 更新数据到数据库
public function update($sql) {
$result = $this->query($sql);
if($result){
return true;
}else{
return false;
}
}
// 删除数据到数据库
public function delete($sql) {
$result = $this->query($sql);
if($result){
return true;
}else{
return false;
}
}
}
使用教程:
先配置数据库相关参数
$config = array(
‘host’=>‘localhost’, //主机名
‘port’=>‘3306’, // 端口
‘username’=>‘root’, // 用户名
‘password’=>‘lianlin’, // 密码
‘charset’=>‘utf8’, // 字符集
‘dbname’=>‘android’, // 数据库名称
);
实例化
$link = MySQLDB::getInstance( $config );
插入数据库
$sql = "insert into user (username,pwd,create_time)values('666','$pwd','$create_time')";
if($link->query($sql)){
echo "执行成功";
}
获取数据库全部数据
$sql = "select * from user;";
$result = $link->getAll($sql); //得到的是二维数组
echo "<table border='1'>";
foreach($result as $row){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
获取单条数据
$sql = "select * from user where id = 1;";
$result = $link->getRow($sql); //得到的是一维数组
if($result){
echo "<br />用户ID为:" . $result['id'];
echo "<br />用户名为:" . $result['username'];
echo "<br />用户密码为:" . $result['pwd'];
echo "<br />创建时间为:" . $result['create_time'];
}
else{
echo "<br />用户不存在";
}
获取总数
$sql = "select count(*) as s from user;";
$result = $link->getOne($sql); //得到的是一个数据
echo "<br />总用户为:" . $result;
更新数据
$sql = "update user set username = '999' where id = 12";
if($link->query($sql)){
echo "执行成功";
}
删除数据库单条数据
$sql = "delete from user where id = 13";
if($link->query($sql)){
echo "执行成功";
}