程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 簡單易用的計數器(數據庫)

簡單易用的計數器(數據庫)

編輯:關於PHP編程

用法
<?
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;
        }
}

}
?> 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved