从一个不错的留言本弄的MysqL数据库操作类,初学PHP的朋友可以参考下
PHP;">
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.'
'.$sql.'
'.$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 .= ''; $str .= '提示
'.$sql.'
'.$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 .= ''; $str .= '