本文实例讲述了一款简单实用的php操作mysql数据库类。分享给大家供大家参考。具体如下:复制代码 代码如下: /* 本款数据库连接类,他会自动加载sql防注入功能,过滤一些敏感的sql查询关键词,同时还可以增加判断字段 show table status的性质与show table类 获取数据库所有表名等。*/ @ini_set('mysql.trace_mode','off'); html' target='_blank'>class mysql { public $dblink; public $pconnect; private $search = array('/union(s*(/*.**/) s*)+select/i', '/load_file(s*(/*.**/) s*)+(/i', '/into(s*(/*.**/) s*)+outfile/i'); private $replace = array('union select', 'load_file (', 'into outfile'); private $rs;
function __construct($hostname,$username,$userpwd,$database,$pconnect=false,$charset='utf8') { define('allowed_htmltags', ' html embed title meta body a p br hr h1 h2 h3 h4 h5 h6 font u i b strong div span ol ul li img table tr td map $this- pconnect=$pconnect; $this- dblink=$pconnect mysql_pconnect($hostname,$username,$userpwd):mysql_connect($hostname,$username,$userpwd); (!$this- dblink||!is_resource($this- dblink)) && fatal_error("connect to the database unsuccessfully!"); @mysql_unbuffered_query("set names {$charset}"); if($this- version() '5.0.1') { @mysql_unbuffered_query("set sql_mode = ''"); } @mysql_select_db($database) or fatal_error("can not select table!"); return $this- dblink; }
function get_maxfield($filed='id',$table) // 获取$table表中$filed字段的最大值 { $r=$this- fetch_one("select {$table}.{$filed} from `{$table}` order by `{$table}`.`{$filed}` desc limit 0,1"); return $r[$filed]; }
if($sql)$sql = "update `$tbname` set $sql where $where"; else return true; } else { $sql = "replace into `$tbname`(`".implode('`,`', array_keys($array))."`) values('".implode("','", $array)."')"; } return $this- query($sql,true); }
function mysql_delete($tbname,$idarray,$filedname='id') { $idwhere=is_array($idarray) implode(',',$idarray):intval($idarray); $where=is_array($idarray) "{$tbname}.{$filedname} in ({$idwhere})":" {$tbname}.{$filedname}={$idwhere}";
return $this- query("delete from {$tbname} where {$where}",true); }
function get_fields($table) { $fields=array(); $result=$this- fetch_all("show columns from `{$table}`"); foreach($result as $val) { $fields[]=$val['field']; } return $fields; }
function get_table_status($database) { $status=array(); $r=$this- fetch_all("show table status from `".$database."`"); /////// show table status的性质与show table类似,不过,可以提供每个表的大量信息。 foreach($r as $v) { $status[]=$v; } return $status; }
function get_one_table_status($table) { return $this- fetch_one("show table status like '$table'"); }