<?php
class MYSQLDB{
private $host;//主机地址
private $port;//端口号
private $user;//用户名
private $pwd;//密码
private $dbname;//数据库名
private $charset;//字符集
private $link;//连接对象
private static $instance;
//构造方法
private function __construct($param){
$this->initParam($param);
$this->initConnect();
}
/*
//析构方法
public function __destruct() {
}
*/
private function __clone(){
}
public static function getInstance($param=array()){
if(!self::$instance instanceof self){//instanceof 用来判断对象是否属于某个类
self::$instance=new self($param);
}
return self::$instance;
}
//初始化参数
private function initParam($param){
$this->host=$param['host']??'127.0.0.1';
$this->port=$param['port']??'3306';
$this->user=$param['user']??'';
$this->pwd=$param['pwd']??'';
$this->dbname=$param['dbname']??'';
$this->charset=$param['charset']??'utf8';
}
private function initConnect(){
$this->link=@mysqli_connect($this->host,$this->user,$this->pwd,$this->dbname,$this->port);
if(mysqli_connect_error()){
echo '数据库连接失败<br/>';
echo "错误信息:".mysqli_connect_error().'<br/>';
echo "错误码:".mysqli_connect_errno().'<br/>';
exit;
}
mysqli_set_charset($this->link,$this->charset);
}
//执行数据库的增删改
private function execute($sql){
if(!$rs=mysqli_query($this->link,$sql)){
echo 'SQL语句执行失败<br/>';
echo "错误信息:".mysqli_error($this->link).'<br/>';
echo "错误码:".mysqli_errno($this->link).'<br/>';
echo "错误的SQL语句".$sql."<br/>";
//exit;
}
return $rs;
}
/*
增 删 改
return bool 成功返回true 失败返回false
*/
public function exec($sql){
$key=strtolower(substr($sql,0,6));
if(in_array($key, array('insert','update','delete'))){
return $this->execute($sql);
}else{
echo "非法访问";
exit;
}
}
//获取自动增长的编号
public function getLastInsertID(){
return mysqli_insert_id($this->link);
}
//查询语句
private function query($sql){
$sqlstart=strtolower(substr($sql,0,6));
if($sqlstart=='select' || $sqlstart=='show' || $sqlstart=='desc'){
return $this->execute($sql);
}else{
echo "非法访问";
exit;
}
}
/*
//执行查询语句,返回二维数组
//type string assoc|num|both
*/
public function fetchAll($sql,$type='assoc'){
$rs=$this->query($sql);
$type=$this->getType($type);
return mysqli_fetch_all($rs,$type);
}
//匹配一维数组
public function fetchRow($sql,$type='assoc'){
$list=$this->fetchAll($sql,$type);
if(!empty($list)){
return $list[0];
}
return array();
}
//匹配一行一列
public function fetchColumn($sql) {
$list=$this->fetchRow($sql,'num');
if(!empty($list))
return $list[0];
return null;
}
//获取匹配类型
private function getType($type) {
switch($type){
case 'num':
return MYSQLI_NUM;
case 'both':
return MYSQLI_BOTH;
default:
return MYSQLI_ASSOC;
}
}
}
上面是mysqlli 实现数据增删改查 引用如下
//自动加载类
spl_autoload_register(function($class_name){
require "./inc/{$class_name}.class.php";
});
//配置参数
$param=array(
'host'=>"127.0.0.1",
'user'=>'root',
'pwd'=>'123456',
'dbname'=>'datagoods'
);
//获取单例
$db=MYSQLDB::getInstance($param);
//修改
//$db->exec("update news set title='aa' where id=18");
/*
//插入
if($db->exec("insert into news values(null,'29号添加的','内容',unix_timestamp())"))
echo '编号是'.$db->getLastInsertID();
*/
//删除
//$db->exec("delete from news where id=19");
//查询
//$list=$db->fetchAll("select * from news");
//查询
$list=$db->fetchRow('select * from news where id=1','aa');
//打印
print_r($list);
//查询多少条数据
echo $db->fetchColumn('select count(*) from news');
下面采用pdo定义的操作数据库类
<?php
class MyPDO{
private $type; //数据库类别
private $host;//主机地址
private $port;//端口号
private $user;//用户名
private $pwd;//密码
private $dbname;//数据库名
private $charset;//字符集
private $pdo;//连接对象
private static $instance;
private function __construct($param){
$this->initParam($param);
$this->initPDO();
$this->initException();
}
private function __clone(){
}
public static function getInstance($param=array()){
if(!self::$instance instanceof self){
self::$instance=new self($param);
}
return self::$instance;
}
//初始化参数
private function initParam($param){
$this->type=$param['type']??'mysql';
$this->host=$param['host']??'127.0.0.1';
$this->port=$param['port']??'3306';
$this->user=$param['user']??'';
$this->pwd=$param['pwd']??'';
$this->dbname=$param['dbname']??'';
$this->charset=$param['charset']??'utf8';
}
//初始化PDO
private function initPDO(){
try{
$dsn="{$this->type}:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}";
$this->pdo=new PDO($dsn,$this->user,$this->pwd);
}catch(PDOException $ex){
$this->showException($ex);
exit;
}
}
//显示异常
private function showException($ex,$sql=''){
if($sql!="")
echo "SQL执行失败<br/> 错误SQL语句是".$sql."<br/>";
echo "错误编号:".$ex->getCode()."<br/>";
echo "错误文件:".$ex->getFile()."<br/>";
echo "错误行号:".$ex->getLine()."<br/>";
echo "错误信息".$ex->getMessage()."<br/>";
}
//设置异常模式
private function initException(){
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
//执行增删改查
public function exec($sql){
try{
return $this->pdo->exec($sql);
}catch(PDOException $ex){
$this->showException($ex,$sql);
exit;
}
}
//获取自动增长的编号
public function lastInsertId(){
return $this->pdo->lastInsertId();
}
//判断匹配类型
private function fetchType($type){
switch ($type){
case 'num':
return PDO::FETCH_NUM;
case 'both':
return PDO::FETCH_BOTH;
case 'obj':
return PDO::FETCH_OBJ;
default:
return PDO::FETCH_ASSOC;
}
}
//获取所有数据,返回二维数组
public function fetchAll($sql,$type='assoc'){
try{
$stmt=$this->pdo->query($sql);
$type= $this->fetchType($type); //获取匹配方法
return $stmt->fetchAll($type);
}catch(PDOException $ex){
$this->showException($ex,$sql);
exit;
}
}
//返回一维数组
public function fetchRow($sql,$type='assoc'){
try{
$stmt=$this->pdo->query($sql);//获取PDOStatement对象
$type= $this->fetchType($type); //获取匹配方法
return $stmt->fetch($type);
}catch(PDOException $ex){
$this->showException($ex,$sql);
exit;
}
}
//返回一行一列
public function fetchColumn($sql){
try{
$stmt=$this->pdo->query($sql);//获取PDOStatement对象
return $stmt->fetchColumn();
}catch(PDOException $ex){
$this->showException($ex,$sql);
exit;
}
}
}
应用如下
<?php
//自动加载类
spl_autoload_register(function($class_name){
require "./inc/{$class_name}.class.php";
});
//配置参数
$param=array(
'host'=>"127.0.0.1",
'user'=>'root',
'pwd'=>'123456',
'dbname'=>'datanews'
);
//获取单例
$mypdo=MyPDO::getInstance($param);
/*
echo $mypdo->exec("delete from news where id=20");
$mypdo->exec("insert into news values(null,'pdo类增加的','杜洛克的',unix_timestamp())");
echo "自动增长编号是".$mypdo->lastInsertId();
*/
$list=$mypdo->fetchAll("select * from news");
echo "<pre>";
var_dump($list);
$list=$mypdo->fetchRow("select * from news where id=22");
var_dump($list);
$count=$mypdo->fetchColumn("select count(*) from news");
echo $count;