简单易用的计数器(数据库)
程序员文章站
2022-08-11 18:43:46
用法 include("counter.php"); counter(__file__);//为文件增加一个计数 if($php_self=...
用法
<?
include("counter.php");
counter(__file__);//为文件增加一个计数
if($php_self=="/index.php")
{
$count=counter("index_count");//为首页增加一个计数
}
else
{
$count=counter("index_count","",0);//取得首页计数
}
echo "你是第$count个访问者";
?>
--------counter.php-----------
<?
if(!isset($php_include_counter_php))
{$php_include_counter_php=__file;
$counter_error_state=0;
$counter_error_msg="";
function counter($file,$query="",$add=1)
{
$db_name="database";
$db_user="username";
$db_pass="password";
$db_table="counter";
if(empty($file))
{
$counter_error_state=-100;
$counter_error_msg="缺少第一个参数或参数为空";
return -100;
}
global $php_self,$query_string,$counter_error_state,$counter_error_msg;
if(empty($db_user)||!$db_user||$db_user=="")$res=@mysql_connect("localhost");
else $res=@mysql_connect("localhost",$db_user,$db_pass);
if(!$res)
{
$counter_error_states=-10;
$counter_error_msg="不能连接数据库";
return -10;
}
if(!@mysql_select_db($db_name))
{
$counter_error_states=-11;
$counter_error_msg="不能选择数据库";
return -11;
}
else
{
if(!$db_res=@mysql_query("select * from ".$db_table))
{
if(!$db_res=@mysql_query("create table ".$db_table." (id integer auto_increment,primary key (id),file varchar(255),query varchar(255),time varchar(255),count int)"))
{
$counter_error_states=-20;
$counter_error_msg="不能创建数据表";
return -20;
}
@mysql_free_result($db_res);
}
$str="select * from ".$db_table." where file=\"".$file."\" and query=\"".$query."\"";
if(!$db_res=@mysql_query($str))
{
$counter_error_states=-30;
$counter_error_msg="不能查询记录";
return -30;
}
$num=@mysql_num_rows($db_res);
if($num>1)
{
$counter_error_states=-40;
$counter_error_msg="发生没有预期的错误=数据行数错误";
return -40;
}
$count=0;
$str="insert ";
$strwhere="";
if($num==1)
{
$row=@mysql_fetch_array($db_res);
@mysql_free_result($db_res);
$count=$row["count"];
$id=$row["id"];
$str="update ";
$strwhere=" where id=$id";
}
if($add<1)return $count;
$count+=$add;
$str.=$db_table." set file=\"".$file."\",query=\"".$query."\",time=\"".date("y;n;d;g;i;s")."\",count=".$count.$strwhere;
$db_res=@mysql_query($str);
if(!$db_res)
{
$counter_error_states=-50;
$counter_error_msg="不能添加或更新记录";
return -50;
}
return $count;
}
}
}
?>
<?
include("counter.php");
counter(__file__);//为文件增加一个计数
if($php_self=="/index.php")
{
$count=counter("index_count");//为首页增加一个计数
}
else
{
$count=counter("index_count","",0);//取得首页计数
}
echo "你是第$count个访问者";
?>
--------counter.php-----------
<?
if(!isset($php_include_counter_php))
{$php_include_counter_php=__file;
$counter_error_state=0;
$counter_error_msg="";
function counter($file,$query="",$add=1)
{
$db_name="database";
$db_user="username";
$db_pass="password";
$db_table="counter";
if(empty($file))
{
$counter_error_state=-100;
$counter_error_msg="缺少第一个参数或参数为空";
return -100;
}
global $php_self,$query_string,$counter_error_state,$counter_error_msg;
if(empty($db_user)||!$db_user||$db_user=="")$res=@mysql_connect("localhost");
else $res=@mysql_connect("localhost",$db_user,$db_pass);
if(!$res)
{
$counter_error_states=-10;
$counter_error_msg="不能连接数据库";
return -10;
}
if(!@mysql_select_db($db_name))
{
$counter_error_states=-11;
$counter_error_msg="不能选择数据库";
return -11;
}
else
{
if(!$db_res=@mysql_query("select * from ".$db_table))
{
if(!$db_res=@mysql_query("create table ".$db_table." (id integer auto_increment,primary key (id),file varchar(255),query varchar(255),time varchar(255),count int)"))
{
$counter_error_states=-20;
$counter_error_msg="不能创建数据表";
return -20;
}
@mysql_free_result($db_res);
}
$str="select * from ".$db_table." where file=\"".$file."\" and query=\"".$query."\"";
if(!$db_res=@mysql_query($str))
{
$counter_error_states=-30;
$counter_error_msg="不能查询记录";
return -30;
}
$num=@mysql_num_rows($db_res);
if($num>1)
{
$counter_error_states=-40;
$counter_error_msg="发生没有预期的错误=数据行数错误";
return -40;
}
$count=0;
$str="insert ";
$strwhere="";
if($num==1)
{
$row=@mysql_fetch_array($db_res);
@mysql_free_result($db_res);
$count=$row["count"];
$id=$row["id"];
$str="update ";
$strwhere=" where id=$id";
}
if($add<1)return $count;
$count+=$add;
$str.=$db_table." set file=\"".$file."\",query=\"".$query."\",time=\"".date("y;n;d;g;i;s")."\",count=".$count.$strwhere;
$db_res=@mysql_query($str);
if(!$db_res)
{
$counter_error_states=-50;
$counter_error_msg="不能添加或更新记录";
return -50;
}
return $count;
}
}
}
?>