首页 > 开发 > PHP > 正文

一款简单实用的php操作mysql数据库类

2024-05-04 22:16:48
字体:
来源:转载
供稿:网友

本文实例讲述了一款简单实用的php操作mysql数据库类。分享给大家供大家参考。具体如下:

代码如下:
/*
本款数据库连接类,他会自动加载sql防注入功能,过滤一些敏感的sql查询关键词,同时还可以增加判断字段 show table status的性质与show table类 获取数据库所有表名等。*/
@ini_set('mysql.trace_mode','off');
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 query($sql,$unbuffered=false)
 {
  //echo $sql.'<br>';
  $this->rs=$unbuffered?mysql_unbuffered_query($sql,$this->dblink):mysql_query($sql,$this->dblink);
  //(!$this->rs||!is_resource($this->rs)) && fatal_error("execute the query unsuccessfully! error:".mysql_error());
  if(!$this->rs)fatal_error('在执行sql语句 '.$sql.' 时发生以下错误:'.mysql_error());
  return $this->rs;
 }
 
 function fetch_one($sql)
 {
  $this->rs=$this->query($sql);
  return dircms_strips教程lashes($this->filter_pass(mysql_fetch_array($this->rs,mysql_assoc)));
 }
 
 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];
 }
 
 function fetch_all($sql)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表