Home > Backend Development > PHP Tutorial > Simple and easy-to-use counter (database)_PHP tutorial

Simple and easy-to-use counter (database)_PHP tutorial

WBOY
Release: 2016-07-21 16:06:18
Original
784 people have browsed it

Usage
include("counter.php");
Counter(__FILE__);//Increase a count for the file
if($PHP_SELF=="/index.php")
{
$count=Counter("INDEX_COUNT");//Add a count to the home page
}
else
{
$count=Counter("INDEX_COUNT","" ,0);//Get homepage count
}
echo "You are the $count visitor";
?>
--------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=-10 0;
$counter_error_msg="The first parameter is missing or empty";
                          return -100;                                                  (empty($ db_user)||!$db_user||$db_user=="")$res=@mysql_connect("localhost");
     else $res=@mysql_connect("localhost",$db_user,$db_pass); >                                                                                                                                   turn -10;
}
if(!@ mysql_select_db($db_name))
                                                                                                                 return -11;
                                                                                        
                                                                                                                                                                                  l_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 ="Cannot create data table";
                                                                                                                                                            
                  @mysql_free_result($db_res);
                                         "" AND query="".$query. """;

if(!$db_res=@mysql_query($str))
> >                 return -30;
                                                 num>1)
                                                                                                             "An unexpected error occurred = wrong number of data rows";
                                                                                                                                 str="INSERT ";
$strWhere=" ";
if($num==1)
{
$row=@mysql_fetch_array($db_res);
                           @mysql_free_result($db_res); 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;
        }
}

}
?> 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/315451.htmlTechArticle用法 ? include(counter.php); Counter(__FILE__);//为文件增加一个计数 if($PHP_SELF==/index.php) { $count=Counter(INDEX_COUNT);//为首页增加一个计数 } else { $count=...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template