Easy-to-use counter (database) _php Tutorial

Source: Internet
Author: User
Usage
Include ("counter.php");
Counter (__file__);//Add a count to 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 home count
}
echo "You are the first $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=-100;
$counter _error_msg= "Missing the first parameter or parameter is empty";
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= "Cannot connect to the database";
return-10;
}
if (! @mysql_select_db ($db _name))
{
$counter _error_states=-11;
$counter _error_msg= "Cannot select a database";
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= "Cannot create data table";
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= "cannot query records";
return-30;
}
$num = @mysql_num_rows ($db _res);
if ($num >1)
{
$counter _error_states=-40;
$counter _error_msg= "Unexpected error occurred = data row number error";
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= "Cannot add or update records";
return-50;
}
return $count;
}
}

}
?>

http://www.bkjia.com/PHPjc/315451.html www.bkjia.com true http://www.bkjia.com/PHPjc/315451.html techarticle usage? include (counter.php); Counter (__file__);//Add a Count if ($PHP _self==/index.php) {$count =counter (index_count) for the file;//Add a count to the first page} else {$count = ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.