<?php
class DaoPdo
{
private $_host;
private $_dbname;
private $_port;
private $_charset;
private $_user;
private $_pwd;
private static $_instance;
private $_pdo;
private function __construct ($arr)
{
$this->_host = isset($arr['host'])?$arr['host']:'';
$this->_dbname = isset($arr['dbname'])?$arr['dbname']:'';
$this->_port = isset($arr['port'])?$arr['port']:'';
$this->_charset = isset($arr['charset'])?$arr['charset']:'';
$this->_user = isset($arr['user'])?$arr['user']:'';
$this->_pwd = isset($arr['pwd'])?$arr['pwd']:'';
if ($this->_host == '' || $this->_dbname == '' || $this->_port == '' || $this->_charset == '' || $this->_user == '' || $this->_pwd == '')
{
die('参数有误');
}
$dsn = "mysql:host=$this->_host;dbname=$this->_dbname;port=$this->_port;charset=$this->_charset";
$this->_pdo = new PDO($dsn,$this->_user,$this->_pwd);
}
public static function getSingleton ($arr)
{
if (!self::$_instance instanceof self)
{
self::$_instance = new self ($arr);
}
return self::$_instance;
}
private function __clone()
{
}
//查询一条数据
public function fetch ($sql)
{
$pdo_statement = $this->_pdo -> query($sql);
$result = $pdo_statement -> fetch(PDO::FETCH_ASSOC);
if (!$result)
{
return $this->_pdo -> errorInfo();
exit;
}
return $result;
}
//查询所有记录
public function fetchAll ($sql)
{
$pdo_statement = $this->_pdo -> query($sql);
$result = $pdo_statement -> fetchAll(PDO::FETCH_ASSOC);
if (!$result)
{
echo $this->_pdo -> errorInfo();
exit;
}
return $result;
}
//增删改数据
public function cud ($sql)
{
$res = $this->_pdo -> exec($sql);
if (!$res)
{
return $this->_pdo -> errorInfo();
exit;
}
return $res;
}
}
class DaoPdo
{
private $_host;
private $_dbname;
private $_port;
private $_charset;
private $_user;
private $_pwd;
private static $_instance;
private $_pdo;
private function __construct ($arr)
{
$this->_host = isset($arr['host'])?$arr['host']:'';
$this->_dbname = isset($arr['dbname'])?$arr['dbname']:'';
$this->_port = isset($arr['port'])?$arr['port']:'';
$this->_charset = isset($arr['charset'])?$arr['charset']:'';
$this->_user = isset($arr['user'])?$arr['user']:'';
$this->_pwd = isset($arr['pwd'])?$arr['pwd']:'';
if ($this->_host == '' || $this->_dbname == '' || $this->_port == '' || $this->_charset == '' || $this->_user == '' || $this->_pwd == '')
{
die('参数有误');
}
$dsn = "mysql:host=$this->_host;dbname=$this->_dbname;port=$this->_port;charset=$this->_charset";
$this->_pdo = new PDO($dsn,$this->_user,$this->_pwd);
}
public static function getSingleton ($arr)
{
if (!self::$_instance instanceof self)
{
self::$_instance = new self ($arr);
}
return self::$_instance;
}
private function __clone()
{
}
//查询一条数据
public function fetch ($sql)
{
$pdo_statement = $this->_pdo -> query($sql);
$result = $pdo_statement -> fetch(PDO::FETCH_ASSOC);
if (!$result)
{
return $this->_pdo -> errorInfo();
exit;
}
return $result;
}
//查询所有记录
public function fetchAll ($sql)
{
$pdo_statement = $this->_pdo -> query($sql);
$result = $pdo_statement -> fetchAll(PDO::FETCH_ASSOC);
if (!$result)
{
echo $this->_pdo -> errorInfo();
exit;
}
return $result;
}
//增删改数据
public function cud ($sql)
{
$res = $this->_pdo -> exec($sql);
if (!$res)
{
return $this->_pdo -> errorInfo();
exit;
}
return $res;
}
}