首页 > 开发 > PHP > 正文

简单易用的计数器(数据库)

2024-05-04 23:01:14
字体:
来源:转载
供稿:网友
用法
<?
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=="")$[email protected]_connect("localhost");
        else $[email protected]_connect("localhost",$db_user,$db_pass);
        if(!$res)
        {
                $counter_error_states=-10;
                $counter_error_msg="不能连接数据库";
                return -10;
        }
        if([email protected]_select_db($db_name))
        {
                $counter_error_states=-11;
                $counter_error_msg="不能选择数据库";
                return -11;
        }
        else
        {
                if(!$[email protected]_query("select * from ".$db_table))
                {
                        if(!$[email protected]_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(!$[email protected]_query($str))
                {
                        $counter_error_states=-30;
                        $counter_error_msg="不能查询记录";
                        return -30;
                }
                $[email protected]_num_rows($db_res);
                if($num>1)
                {
                        $counter_error_states=-40;
                        $counter_error_msg="发生没有预期的错误=数据行数错误";
                        return -40;
                }
                $count=0;
                $str="insert ";
                $strwhere="";
                if($num==1)
                {
                        $[email protected]_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;
                $[email protected]_query($str);
                if(!$db_res)
                {
                        $counter_error_states=-50;
                        $counter_error_msg="不能添加或更新记录";
                        return -50;
                }
                return $count;
        }
}

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