从一个不错的留言本弄的mysql数据库操作类

yipeiwu_com5年前Mysql基础

从一个不错的留言本弄的mysql数据库操作类,初学php的朋友可以参考下

<?php 
class mysql{ 
  var $querynum = 0; 
  function connect($dbhost, $dbuser, $dbpw, $dbname = '',$dbcharset='') { 
    if(!@mysql_connect($dbhost, $dbuser, $dbpw)) { 
      $this->show('Can not connect to MySQL server'); 
      return false; 
    } 
    if($dbname) { 
      $this->select_db($dbname); 
    } 
    if($this->version() > '4.1' && $dbcharset) { 
      $this->query("SET NAMES '".$dbcharset."'"); 
    } 
    return true; 
  } 
  function select_db($dbname) { 
    return mysql_select_db($dbname); 
  } 
  function fetch_array($query, $result_type = MYSQL_ASSOC) { 
    return @mysql_fetch_array($query, $result_type); 
  } 
  function query($sql, $type = '') { 
    if(!($query = mysql_query($sql))) $this->show('MySQL Query Error', $sql); 
    $this->querynum++; 
    return $query; 
  } 
  function affected_rows() { 
    return mysql_affected_rows(); 
  } 
  function result($query, $row) { 
    return mysql_result($query, $row); 
  } 
  function num_rows($query) { 
    return @mysql_num_rows($query); 
  } 
  function num_fields($query) { 
    return mysql_num_fields($query); 
  } 
  function free_result($query) { 
    return mysql_free_result($query); 
  } 
  function insert_id() { 
    return mysql_insert_id(); 
  } 
  function fetch_row($query) { 
    return mysql_fetch_row($query); 
  } 
  function version() { 
    return mysql_get_server_info(); 
  } 
  function close() { 
    return mysql_close(); 
  } 
  function error() { 
    return mysql_error(); 
  } 
  function show($message = '', $sql = '') { 
    if(!$sql) echo $message; 
    else echo $message.'<br>'.$sql.'<br>'.$this->error(); 
  } 
} 
class page extends mysql{ 
  function pagination($sql,$maxnum,$page,$maxpages,$pagepre,$ext=''){ 
    global $sum,$stail,$link,$lmid,$ltail,$curpage;//$ext='&class=3' 
    $SELF = $_SERVER['PHP_SELF']; 
    $query = $this->query($sql); 
    $rows = $this->fetch_array($query,MYSQL_NUM); 
    $totalrows = $rows[0]; 
    $totalpages = ceil($totalrows/$maxnum); 
    $startnum = ($page - 1)*$maxnum; 
    $string = $sum.$totalrows.$stail.$sum.$page."/".$totalpages.$stail; 
    if($page != 1){ 
      $string .= $link.$SELF."?page=1".$ext.$lmid."|‹".$ltail; 
      $string .= $link.$SELF.'?page='.($page - 1).$ext.$lmid."‹‹".$ltail; 
    } 
    if($maxpages>=$totalpages){ 
      $pgstart = 1;$pgend = $totalpages; 
    } 
    elseif(($page-$pagepre-1+$maxpages)>$totalpages){ 
      $pgstart = $totalpages - $maxpages + 1; 
      $pgend = $totalpages; 
    } 
    else{ 
      $pgstart=(($page<=$pagepre)?1:($page-$pagepre)); 
      $pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1)); 
    } 
    for($pg=$pgstart;$pg<=$pgend;$pg++){ 
      if($pg == $page){ 
        $string .= $curpage.$SELF."?page=".$pg.$ext.$lmid.$pg.$ltail; 
      } 
      else $string .= $link.$SELF."?page=".$pg.$ext.$lmid.$pg.$ltail; 
    } 
    if($page != $totalpages){ 
      $string .= $link.$SELF.'?page='.($page + 1).$ext.$lmid."››".$ltail; 
      $string .= $link.$SELF.'?page='.$totalpages.$ext.$lmid."›|".$ltail; 
    } 
    return $string; 
  } 
} 
function html($str,$allowhtml=0){ 
  $str = get_magic_quotes_gpc()?$str:addslashes($str); 
  if(!$allowhtml){$str = htmlspecialchars($str);} 
  return $str; 
} 
function dehtml($str,$allowhtml=0){ 
  $str = stripslashes($str); 
  if(!$allowhtml){$str = nl2br($str);} 
  return $str; 
} 
function deip($str){ 
  $arr = explode('.',$str); 
  $str = $arr[0].'.'.$arr[1].'.'.$arr[2].'.*'; 
  return $str; 
} 
function setting($login=false){ 
  global $db,$mydbpre; 
  if($login) $sql = "select * from {$mydbpre}setting"; 
  else $sql = "select * from {$mydbpre}setting where keyword != 'username' and keyword != 'password'"; 
  $query = $db->query($sql); 
  while($row = $db->fetch_array($query)){ 
    $arr[$row['keyword']] = $row['val']; 
    if($row['keyword'] == 'name') $arr['title'] = $row['val'].' - Powered by PHPfans'; 
  } 
  return $arr; 
} 
function checkcontent($content){ 
  global $db,$mydbpre,$mearr; 
  $arrword = $arrip = array(); 
  $sql = "select * from {$mydbpre}ban where b_type != 0"; 
  $query = $db->query($sql); 
  while($row = $db->fetch_array($query)){ 
    if($row['b_type'] == 1) $arrword[] = $row['b_val']; 
    elseif($row['b_type'] == 2) $arrip[] = $row['b_reval']; 
  } 
  foreach($arrip as $value){ 
    $value = preg_quote($value,'/'); 
    $value = str_replace('\*','\d{1,3}',$value); 
    if(preg_match('/^('.$value.')$/',$_SERVER['REMOTE_ADDR'])){ 
      showmessage($mearr[8],'index.php'); 
    } 
  } 
  foreach($arrword as $val){ 
    $val = preg_quote($val,'/'); 
    if(preg_match('/'.$val.'/',$content)){ 
      showmessage($mearr[7],'index.php'); 
    } 
  } 
} 
function replaceword($content){ 
  global $db,$mydbpre; 
  $sql = "select * from {$mydbpre}ban where b_type = 0"; 
  $query = $db->query($sql); 
  while($row = $db->fetch_array($query)){ 
    $content = str_replace($row['b_val'],$row['b_reval'],$content); 
  } 
  return $content; 
} 
function showadmin($message,$referer=''){ 
  $str = ''; 
  if($referer != '') $str .= '<meta http-equiv="refresh" content="2;URL='.$referer.'" />'; 
  $str .= ' 
 <table width="45%" align="center" style="border:1px solid #eeeeee; margin-top:50px;"> 
 <tr bgcolor="#CCCCCC"> 
  <td height="25">信息提示</td> 
 </tr> 
 <tr align="center"> 
  <td style="padding:10px 0 10px 0">'.$message; 
  if($referer != '') $str .= '<br /> 
 <br /> 
 <a href="'.$referer.'">如果你的页面没有跳转,请点这里</a>'; 
  $str .= ' 
 </td> 
 </tr> 
 </table>'; 
  echo $str; 
  exit; 
} 
function showmessage($message,$referer=''){ 
  global $setting,$db; 
  $start = gettime(); 
  echo <<<EOT 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gbk" /> 
<title>php爱好者留言簿_提示信息</title> 
<link type="text/css" href="guest.css" rel="stylesheet"> 
EOT; 
  if($referer != '') echo '<meta http-equiv="Refresh" content="2;URL='.$referer.'" />'; 
  echo <<<EOT 
</head> 
<body> 
<center> 
EOT; 
  require_once('header.html'); 
  echo <<<EOT 
<div class="message"> 
<h1>php爱好者留言簿 提示信息</h1> 
{$message} 
EOT; 
  if($referer != ''){ 
    echo <<<EOT 
<br /> 
<a href="{$referer}">如果您的浏览器没有自动跳转,请点击这里</a> 
</div> 
EOT; 
} 
require_once('footer.html'); 
echo <<<EOT 
</center> 
</body> 
</html> 
EOT; 
exit; 
} 
function gettime() 
{ 
  $t = explode(" ",microtime()); 
  return $t[1] + $t[0]; 
} 
function checklogin($exit=true){ 
  if(!isset($_COOKIE['islogin']) || $_COOKIE['islogin'] != 1){ 
    if($exit) { 
      echo "请先登陆,谢谢。"; 
      exit; 
    } 
  } 
  else setcookie('islogin',1,time()+60*20); 
} 
?> 

好了,本文到此结束,希望对大家有所帮助!

相关文章

php实现mysql数据库连接操作及用户管理

文件列表。。文件内容。。 dbconn.php userListt.php editUser.php editDo.php detailUser.php deleteUser.php a...

PHP实现的通过参数生成MYSQL语句类完整实例

本文实例讲述了PHP实现的通过参数生成MYSQL语句类。分享给大家供大家参考,具体如下: 这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELE...

PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\

PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\

今天在家启动PHP环境的时候,突然发现不能加载php_mysqli.dll了,网上找了一圈,没有解决方案! 在群里面受一哥们儿的启发,竟然解决了,如果你的问题还没有解决,请按下面的方法试...

php+mysql开发中的经验与常识小结

本文总结了php+mysql开发中的经验与常识。分享给大家供大家参考,具体如下: 一、基础规范 (1)尽量使用 InnoDB 存储引擎 支持事务、行级锁、并发性能更好,CPU 及内存缓...

PHP中执行MYSQL事务解决数据写入不完整等情况

近来稍有时间研究了下MYSQL中的事务操作,在很多场合下很是适用,譬如在注册的时候需要初始化很多张关联表的时候,问答回复的时候需要至少同时操作两张表,这些都会在某些时候只能成功更新一张表...