<?php
//定义连接数据库的类,并返回链接后的资源
//实现单例模式
//执行普通增删改非返回结果集语句
//执行select语句并可以返回3种类型的数据
//单行结果(一维数组),多行结果(二维数组)
//单行单例(单个数据)
class mysqlDB{
public $host;
public $port;
public $username;
public $password;
public $charset;
public $dbname;
//连接结果
private static $link;
private $resource;
//单例方法
public static function getInstance($config){
if(!isset(self::$link)){
self::$link = new self($config);
}
return self::$link;
}
//禁止new
private function __construct($config){
$this->host = isset($config['host'])?$config['host']:'localhost';
$this->port = isset($config['port'])?$config['port']:'8889';
$this->username = isset($config['username'])?$config['username']:'root';
$this->password = isset($config['password'])?$config['password']:'root';
$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);
}
//禁止clone
private function __clone(){}
public function connect(){
$this->resource = mysql_connect("$this->host:$this->port","$this->username","$this->password") or die("连接数据库失败");
}
public function setCharset($charset){
mysql_set_charset($charset,$this->resource);
}
public function selectDb($dbname){
mysql_select_db($dbname,$this->resource);
}
/**
* 执行最基本sql语句
* @param 返回错误代码
* @return 返回执行结果
*/
public function query($sql){
//执行失败
if(!$result = mysql_query($sql,$this->resource)){
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;
}
/**
* 功能:返回一行数据作为一维数组
* 参数:$sql 字符串类型 select语句
*/
public function getRow($sql){
$result = $this -> query($sql);
if($rec2 = mysql_fetch_assoc($result)){
//如果fetch出来有数据,返回一维数组
return $rec2;
}
return false;
}
/**
* 功能:返回select的第一行第一列
* 参数:$sql 字符串类型 select语句
*/
public function getOne($sql){
$result = $this -> query($sql);
$rec = mysql_fetch_row($result);//返回下标为数字的数组
if($result == false){
return false;
}
return $rec[0];
}
}
?>
使用
<?
require 'SQLDB.class.php'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User</title>
</head>
<body>
<?php
$config = array(
'host'=>'localhost',
'port'=>'8889',
'username'=>'root',
'password'=>'root',
'charset'=>'utf8',
'dbname'=>'db1'
);
$link = mysqlDB::getInstance($config);
//任务1:插入数据
$sql = "insert into money(zhanghu,cunkuan) values('test3',10000);";
// if($link->query($sql)){
// echo "执行成功";
// }
//任务2:获取所有账户信息
$sql = "select * from money;";
$result = $link->getAll($sql);
echo "<pre>";
//print_r($result);
//放入表格
echo "<table border='1'>";
foreach ($result as $row) {
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
echo "</pre>";
//任务3:获取某个账户信息(一行数据)
$sql = "select * from money where id = 55;";
$result = $link -> getRow($sql);
if($result){
echo "<br/>账户ID为:".$result['id'];
echo "<br/>账户名为:".$result['zhanghu'];
echo "<br/>存款为:".$result['cunkuan'];
}else{
echo "账户不存在";
}
//任务4:获取该银行的所有存款总和:
$sql = "select sum(cunkuan) as s from money;";
$result = $link -> getOne($sql);
echo "<br/> 总存款为:".$result;
?>
</body>
</html>