提供mysql的增、删、改、查等操作
[代码][php]代码
class pdomysql {
public static $dbtype = 'mysql';
public static $dbhost = '';
public static $dbport = '';
public static $dbname = '';
public static $dbuser = '';
public static $dbpass = '';
public static $charset = '';
public static $stmt = null;
public static $db = null;
public static $connect = true; // 是否長连接
public static $debug = false;
private static $parms = array ();
/**
* 构造函数
*/
public function __construct() {
self::$dbtype = 'mysql';
self::$dbhost = host;
self::$dbport = '3306';
self::$dbname = 'tion';
self::$dbuser = 'manager';
self::$dbpass = '123';
self::$connect = true;
self::$charset = 'utf8';
self::connect ();
self::$db->setattribute ( pdo::mysql_attr_use_buffered_query, true );
self::$db->setattribute ( pdo::attr_emulate_prepares, true );
self::execute ( 'set names ' . self::$charset );
}
/**
* 析构函数
*/
public function __destruct() {
self::close ();
}
/**
* *******************基本方法开始********************
*/
/**
* 作用:连結数据库
*/
public function connect() {
try {
self::$db = new pdo ( self::$dbtype . ':host=' . self::$dbhost . ';port=' . self::$dbport . ';dbname=' . self::$dbname, self::$dbuser, self::$dbpass, array (
pdo::attr_persistent => self::$connect
) );
} catch ( pdoexception $e ) {
die ( "connect error infomation:" . $e->getmessage () );
}
}
/**
* 关闭数据连接
*/
public function close() {
self::$db = null;
}
/**
* 對字串進行转義
*/
public function quote($str) {
return self::$db->quote ( $str );
}
/**
* 作用:获取数据表里的欄位
* 返回:表字段结构
* 类型:数组
*/
public function getfields($table) {
self::$stmt = self::$db->query ( "describe $table" );
$result = self::$stmt->fetchall ( pdo::fetch_assoc );
self::$stmt = null;
return $result;
}
/**
* 作用:获得最后insert的主鍵id
* 返回:最后insert的主鍵id
* 类型:数字
*/
public function getlastid() {
return self::$db->lastinsertid ();
}
/**
* 作用:執行insert\update\delete
* 返回:执行語句影响行数
* 类型:数字
*/
public function execute($sql) {
self::getpdoerror ( $sql );
return self::$db->exec ( $sql );
}
/**
* 获取要操作的数据
* 返回:合併后的sql語句
* 类型:字串
*/
private function getcode($table, $args) {
$code = '';
if (is_array ( $args )) {
foreach ( $args as $k => $v ) {
if ($v == '') {
continue;
}
$code .= "`$k`='$v',";
}
}
$code = substr ( $code, 0, - 1 );
return $code;
}
public function optimizetable($table) {
$sql = "optimize table $table";
self::execute ( $sql );
}
/**
* 执行具体sql操作
* 返回:运行結果
* 类型:数组
*/
private function _fetch($sql, $type) {
$result = array ();
self::$stmt = self::$db->query ( $sql );
self::getpdoerror ( $sql );
self::$stmt->setfetchmode ( pdo::fetch_assoc );
switch ($type) {
case '0' :
$result = self::$stmt->fetch ();
break;
case '1' :
$result = self::$stmt->fetchall ();
break;
case '2' :
$result = self::$stmt->rowcount ();
break;
}
self::$stmt = null;
return $result;
}
/**
* *******************基本方法結束********************
*/
/**
* *******************sql操作方法开始********************
*/
/**
* 作用:插入数据
* 返回:表內記录
* 类型:数组
* 參数:$db->insert('$table',array('title'=>'zxsv'))
*/
public function add($table, $args) {
$sql = "insert into `$table` set ";
$code = self::getcode ( $table, $args );
$sql .= $code;
return self::execute ( $sql );
}
/**
* 修改数据
* 返回:記录数
* 类型:数字
* 參数:$db->update($table,array('title'=>'zxsv'),array('id'=>'1'),$where
* ='id=3');
*/
public function update($table, $args, $where) {
$code = self::getcode ( $table, $args );
$sql = "update `$table` set ";
$sql .= $code;
$sql .= " where $where";
return self::execute ( $sql );
}
/**
* 作用:刪除数据
* 返回:表內記录
* 类型:数组
* 參数:$db->delete($table,$condition = null,$where ='id=3')
*/
public function delete($table, $where) {
$sql = "delete from `$table` where $where";
return self::execute ( $sql );
}
/**
* 作用:获取單行数据
* 返回:表內第一条記录
* 类型:数组
* 參数:$db->fetone($table,$condition = null,$field = '*',$where ='')
*/
public function fetone($table, $field = '*', $where = false) {
$sql = "select {$field} from `{$table}`";
$sql .= ($where) ? " where $where" : '';
return self::_fetch ( $sql, $type = '0' );
}
/**
* 作用:获取所有数据
* 返回:表內記录
* 类型:二維数组
* 參数:$db->fetall('$table',$condition = '',$field = '*',$orderby = '',$limit
* = '',$where='')
*/
public function fetall($table, $field = '*', $orderby = false, $where = false) {
$sql = "select {$field} from `{$table}`";
$sql .= ($where) ? " where $where" : '';
$sql .= ($orderby) ? " order by $orderby" : '';
return self::_fetch ( $sql, $type = '1' );
}
/**
* 作用:获取單行数据
* 返回:表內第一条記录
* 类型:数组
* 參数:select * from table where id='1'
*/
public function getone($sql) {
return self::_fetch ( $sql, $type = '0' );
}
/**
* 作用:获取所有数据
* 返回:表內記录
* 类型:二維数组
* 參数:select * from table
*/
public function getall($sql) {
return self::_fetch ( $sql, $type = '1' );
}
/**
* 作用:获取首行首列数据
* 返回:首行首列欄位值
* 类型:值
* 參数:select `a` from table where id='1'
*/
public function scalar($sql, $fieldname) {
$row = self::_fetch ( $sql, $type = '0' );
return $row [$fieldname];
}
/**
* 获取記录总数
* 返回:記录数
* 类型:数字
* 參数:$db->fetrow('$table',$condition = '',$where ='');
*/
public function fetrowcount($table, $field = '*', $where = false) {
$sql = "select count({$field}) as num from $table";
$sql .= ($where) ? " where $where" : '';
return self::_fetch ( $sql, $type = '0' );
}
/**
* 获取記录总数
* 返回:記录数
* 类型:数字
* 參数:select count(*) from table
*/
public function getrowcount($sql) {
return self::_fetch ( $sql, $type = '2' );
}
/**
* *******************sql操作方法結束********************
*/
/**
* *******************错误处理开始********************
*/
/**
* 設置是否为调试模式
*/
public function setdebugmode($mode = true) {
return ($mode == true) ? self::$debug = true : self::$debug = false;
}
/**
* 捕获pdo错误信息
* 返回:出错信息
* 类型:字串
*/
private function getpdoerror($sql) {
self::$debug ? self::errorfile ( $sql ) : '';
if (self::$db->errorcode () != '00000') {
$info = (self::$stmt) ? self::$stmt->errorinfo () : self::$db->errorinfo ();
echo (self::sqlerror ( 'mysql query error', $info [2], $sql ));
exit ();
}
}
private function getstmterror($sql) {
self::$debug ? self::errorfile ( $sql ) : '';
if (self::$stmt->errorcode () != '00000') {
$info = (self::$stmt) ? self::$stmt->errorinfo () : self::$db->errorinfo ();
echo (self::sqlerror ( 'mysql query error', $info [2], $sql ));
exit ();
}
}
/**
* 寫入错误日志
*/
private function errorfile($sql) {
echo $sql . '<br />';
$errorfile = _root . './dberrorlog.php';
$sql = str_replace ( array (
"\n",
"\r",
"\t",
" ",
" ",
" "
), array (
" ",
" ",
" ",
" ",
" ",
" "
), $sql );
if (! file_exists ( $errorfile )) {
$fp = file_put_contents ( $errorfile, "<?php exit('access denied'); ?>\n" . $sql );
} else {
$fp = file_put_contents ( $errorfile, "\n" . $sql, file_append );
}
}
/**
* 作用:运行错误信息
* 返回:运行错误信息和sql語句
* 类型:字符
*/
private function sqlerror($message = '', $info = '', $sql = '') {
$html = '';
if ($message) {
$html .= $message;
}
if ($info) {
$html .= 'sqlid: ' . $info ;
}
if ($sql) {
$html .= 'errorsql: ' . $sql;
}
throw new exception($html);
}
/**
* *******************错误处理結束********************
*/
}