📄 pdomysql.php
字号:
<?php
/*
*/
class Easy_Db_PDOMysql implements Easy_Db_Interface{
/*
链接数据库
*/
protected static $db;
public $result = null;
public $sql;
public $charset = 'GBK';
protected $dbpass, $dbuser;
public function __construct(){
$array = func_get_arg(0);
$this->dbhost = (string)$array['dbhost'];
$this->dbname = (string)$array['dbname'];
$this->dbuser = (string)$array['dbuser'];
$this->dbpass = (string)$array['dbpass'];
$this->charset = $array['dbchar'] ? $array['dbchar'] : 'UTF-8';
try {
if (class_exists('PDO')===true){
self::$db = new PDO(
'mysql:host='.$this->dbhost.';dbname='.$this->dbname, $this->dbuser, $this->dbpass);
self::$db->exec('set names '.$this->charset);
}else{
exit('系统不支持PDO驱动!');
}
}catch (PDOException $e) {
die( "Mysql Error!: " . $e->getMessage() . "<br />");
}
}
/*
数据插入
@param $table string
@param $array array()
*/
public function insert($table, $data){
if (is_array($data)){
foreach($data as $key=>$value){
$str.= $str ? ",`$key`" : "`$key`";
$val.= $val ? ','.$this->quote(trim($value)) : $this->quote(trim($value));
}
$this->sql = "INSERT INTO $table($str) VALUES($val)";
$row = self::$db->exec($this->sql); //Return number of rows that were Insert
self::error(self::$db->errorInfo());
return $row;
}else{
exit($table.'传入的数据不是数组');
}
}
public function lastInsertId(){
return self::$db->lastInsertId();
}
/*
执行删除记录动作
@param $table 表名称
@where 删除记录的条件
*/
public function delete($table, $where=null){
if (is_array($where)){
foreach($where as $key=>$value){
$wheres.= " AND `$key` = ".$this->quote($value);
}
}else{
$wheres = 'AND '. $where;
}
$this->sql = "DELETE FROM $table WHERE 1=1 $wheres";
unset($array, $wheres, $table, $debug, $params);
return self::$db->exec($this->sql);
}
/*
更新记录数据
*/
public function update($table, $data, $where=null){
if (is_array($data)){
foreach($data as $key=>$value){
$str.= (empty($str)===false) ? ",`$key`=".$this->quote($value) : "`$key`=".$this->quote($value);
}
}else{
$str = $data;
}
unset($key,$value);
if (is_array($where)){
foreach($where as $key=>$value){
$wheres.= " AND `$key` = ".$this->quote($value);
}
}elseif (is_null($where)===false){
$wheres = 'AND '. $where;
}else{
unset($wheres);
}
$this->sql = "UPDATE ".$table." SET $str WHERE 1=1 $wheres";
unset($wheres);
$rows = self::$db->exec($this->sql);
self::error(self::$db->errorInfo());
return $rows;
}
/*
执行单条SQL更新语句
$sql string
return int
*/
public function Exec($sql, $type=null){
$this->sql = $sql;
if ($this->sql){
$rows = self::$db->exec($this->sql);
self::error(self::$db->errorInfo());
return $rows;
}
}
/*
返回多条数据库记录
*/
public function fetAll($sql, $type=null){
$sth = $this->query($sql, $type=null);
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
/*
返回单条数据库记录数据
*/
public function fetRow($sql, $type=null)
{
$sth = $this->query($sql, $type);
return $sth->fetch(PDO::FETCH_ASSOC);
}
/*
返回记录总数
*/
public function getCount($sql){
$this->sql = preg_replace("/^select (.+?) from/is", "SELECT COUNT(*) as total FROM", $sql);
$sth = $this->query($this->sql);
$row = $sth->fetch(PDO::FETCH_ASSOC);
unset($sth, $args);
return $row['total'];
}
/*
获取指定字段的值
$pos int
return string
*/
public function fetOne($sql)
{
}
public function query($sql, $type=null)
{
$this->sql = $sql;
$this->result = null;
$sth = self::$db->prepare($this->sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
if (is_array($type)){
$sth->execute($type);
}else{
$sth->execute();
}
self::error($sth->errorInfo());
return $sth;
}
public function beginTransaction()
{
$this->db->beginTransaction();
}
/*
事务提交
*/
public function commit()
{
self::$db->commit();
}
public function rollBack(){
self::$db->rollBack();
}
public function quote($string){
return self::$db->quote($string);
}
public function error($error){
if ( intval(@array_shift($error)) !== 0){
echo '<xmlprc>数据库查询出错了!';
echo 'SQL:'.$this->sql;
echo '错误描述'.print_r($params).'</xmlprc>';
}
}
public function showtable($dbname=null){
if (is_null($dbname)){
$dbname = $this->dbname;
//$this->query('SHOW TABLES FROM '.$dbname);
$data = array();
foreach($this->fetAll('SHOW TABLES FROM '.$dbname) as $row){
$data[] = current(array_values($row));
}
}
return $data;
}
public function debug($bool=false){
var_dump(self);
}
public function select(){
return new Easy_Db_Select();
}
function __call($method, $args){
//echo $func;
if(method_exists($this, $func) === false){
exit('调用了无效的类方法名:'.$method.'<br>错误行数:'.__LINE__.'<br>错误文件'.__FILE__);
}else{
return $this->$func($args);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -