欢迎来到福编程网,本站提供各种互联网专业知识!

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

发布时间:2007-09-02 作者: 来源:转载
从一个不错的留言本弄的mysql数据库操作类,初学php的朋友可以参考下复制代码代码如下:
从一个不错的留言本弄的mysql数据库操作类,初学php的朋友可以参考下
复制代码 代码如下:
classmysql{
var$querynum=0;
functionconnect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset=''){
if(!@mysql_connect($dbhost,$dbuser,$dbpw)){
$this->show('CannotconnecttoMySQLserver');
returnfalse;
}
if($dbname){
$this->select_db($dbname);
}
if($this->version()>'4.1'&&$dbcharset){
$this->query("SETNAMES'".$dbcharset."'");
}
returntrue;
}

functionselect_db($dbname){
returnmysql_select_db($dbname);
}

functionfetch_array($query,$result_type=MYSQL_ASSOC){
return@mysql_fetch_array($query,$result_type);
}

functionquery($sql,$type=''){
if(!($query=mysql_query($sql)))$this->show('MySQLQueryError',$sql);
$this->querynum++;
return$query;
}

functionaffected_rows(){
returnmysql_affected_rows();
}

functionresult($query,$row){
returnmysql_result($query,$row);
}

functionnum_rows($query){
return@mysql_num_rows($query);
}

functionnum_fields($query){
returnmysql_num_fields($query);
}

functionfree_result($query){
returnmysql_free_result($query);
}

functioninsert_id(){
returnmysql_insert_id();
}

functionfetch_row($query){
returnmysql_fetch_row($query);
}

functionversion(){
returnmysql_get_server_info();
}

functionclose(){
returnmysql_close();
}

functionerror(){
returnmysql_error();
}

functionshow($message='',$sql=''){
if(!$sql)echo$message;
elseecho$message.'

'.$sql.'

'.$this->error();
}
}

classpageextendsmysql{
functionpagination($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;
}
}

functionhtml($str,$allowhtml=0){
$str=get_magic_quotes_gpc()?$str:addslashes($str);
if(!$allowhtml){$str=htmlspecialchars($str);}
return$str;
}

functiondehtml($str,$allowhtml=0){
$str=stripslashes($str);
if(!$allowhtml){$str=nl2br($str);}
return$str;
}

functiondeip($str){
$arr=explode('.',$str);
$str=$arr[0].'.'.$arr[1].'.'.$arr[2].'.*';
return$str;
}

functionsetting($login=false){
global$db,$mydbpre;
if($login)$sql="select*from{$mydbpre}setting";
else$sql="select*from{$mydbpre}settingwherekeyword!='username'andkeyword!='password'";
$query=$db->query($sql);
while($row=$db->fetch_array($query)){
$arr[$row['keyword']]=$row['val'];
if($row['keyword']=='name')$arr['title']=$row['val'].'-PoweredbyPHPfans';
}
return$arr;
}

functioncheckcontent($content){
global$db,$mydbpre,$mearr;
$arrword=$arrip=array();
$sql="select*from{$mydbpre}banwhereb_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($arripas$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($arrwordas$val){
$val=preg_quote($val,'/');
if(preg_match('/'.$val.'/',$content)){
showmessage($mearr[7],'index.php');
}
}
}

functionreplaceword($content){
global$db,$mydbpre;
$sql="select*from{$mydbpre}banwhereb_type=0";
$query=$db->query($sql);
while($row=$db->fetch_array($query)){
$content=str_replace($row['b_val'],$row['b_reval'],$content);
}
return$content;
}

functionshowadmin($message,$referer=''){
$str='';
if($referer!='')$str.='';
$str.='


信息提示


'.$message;
if($referer!='')$str.='



如果你的页面没有跳转,请点这里';

$str.='


';
echo$str;
exit;
}

functionshowmessage($message,$referer=''){
global$setting,$db;
$start=gettime();
echo<<



php爱好者留言簿_提示信息

EOT;
if($referer!='')echo'';
echo<<


EOT;
require_once('header.html');
echo<<

php爱好者留言簿提示信息


{$message}
EOT;
if($referer!=''){
echo<<


如果您的浏览器没有自动跳转,请点击这里


EOT;
}
require_once('footer.html');
echo<<




EOT;
exit;
}

functiongettime()
{
$t=explode("",microtime());
return$t[1]+$t[0];
}

functionchecklogin($exit=true){
if(!isset($_COOKIE['islogin'])||$_COOKIE['islogin']!=1){
if($exit){
echo"请先登陆,谢谢。";
exit;
}
}
elsesetcookie('islogin',1,time()+60*20);
}
?>

相关推荐