[PHP] 纯文本查看 复制代码 <?php
/**
* DB驱动
*/
class DBLibrary
{
private $obj;
/**
* [__construct 构造方法]
* [url=home.php?mod=space&uid=952169]@Param[/url] [string] $host [连接地址]
* @param [string] $name [数据库名]
* @param [string] $user [用户名]
* @param [string] $pwd [用户密码]
* @param [boolean] $connect [是否长连接]
*/
private function __construct($host, $name, $user, $pwd, $connect)
{
if(empty($host) || empty($name) || empty($user) || empty($pwd)) exit('Param Error');
try
{
$charset = C('charset');
$charset = empty($charset) ? 'utf8' : str_replace('-', '', $charset);
@$this->obj = new PDO("mysql:dbname={$name};host={$host};charset={$charset}", $user, $pwd, array(PDO::ATTR_PERSISTENT=>$connect));
/* 属性设置 */
$this->obj->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
}
catch(PDOException $e)
{
exit('DB Connect Fails');
}
}
/**
* [SetInstance 静态方法]
* @param string $type [请求类型]
*/
public static function SetInstance($host, $name, $user, $pwd, $connect = false)
{
static $object = null;
if(!is_object($object)) $object = new self($host, $name, $user, $pwd, $connect);
return $object;
}
/**
* [Query 查询数据对象]
* @param [string] $sql [sql查询语句]
* [url=home.php?mod=space&uid=155549]@Return[/url] [obj] [对象]
*/
private function Query($sql)
{
if(empty($sql)) return '';
try
{
$rs = $this->obj->query($sql);
if(!is_object($rs)) exit('DB Non Object Error');
$rs->setFetchMode(PDO::FETCH_ASSOC);
return $rs;
}
catch(PDOException $e)
{
exit('DB Resources Error');
}
}
/**
* [GetAll 获取所有数据]
* @param [string] $sql [sql查询语句]
* @return [array|空字符串] [二维数组或空字符串]
*/
public function GetAll($sql)
{
$data = $this->Query($sql)->fetchAll();
return empty($data) ? '' : $data;
}
/**
* [GetRow 获取一行的数据]
* @param [string] $sql [sql查询语句]
* @return [array|空字符串] [一维数组或空字符串]
*/
public function GetRow($sql)
{
$data = $this->Query($sql)->fetch();
return empty($data) ? '' : $data;
}
/**
* [GetOne 获取一个字段的数据]
* @param [string] $sql [sql查询语句]
* @return [string|空字符串] [字符串或空字符串]
*/
public function GetOne($sql)
{
$data = $this->Query($sql)->fetchColumn();
return empty($data) ? '' : $data;
}
/**
* [GetCol 获取所有数据的一列数据]
* @param [string] $sql [sql查询语句]
* @return [array|空字符串] [一维数组或空字符串]
*/
public function GetCol($sql)
{
if(empty($sql)) return '';
$data = array();
$rs = $this->obj->query($sql);
$rs->setFetchMode(PDO::FETCH_NUM);
while($row = $rs->fetch())
{
$data[] = $row[0];
}
return empty($data) ? '' : $data;
}
/**
* [Exec 数据更新 删除]
* @param [string] $sql [sql操作语句]
* @return [boolean] [成功true, 失败false]
*/
public function Exec($sql)
{
if($this->obj->exec($sql)) return true;
return false;
}
/**
* [Insert 数据插入]
* @param [string] $sql [sql插入语句]
* @return [int] [成功返回自增id(表无自增id则0), 失败-1]
*/
public function Insert($sql)
{
if($this->obj->exec($sql)) return $this->obj->lastInsertId();
return -1;
}
/**
* [StartTrans 开启事物]
*/
public function StartTrans()
{
$this->obj->beginTransaction();
}
/**
* [Commit 提交事物]
*/
public function Commit()
{
$this->obj->commit();
}
/**
* [RollBack 回滚事物]
*/
public function RollBack()
{
$this->obj->rollBack();
}
}
?> |