当前位置: 首页 > 图文教程 > 网络编程 > PHP > 简单易用的计数器(数据库)

PHP
PHP 手机归属地查询 api
php 自写函数代码 获取关键字 去超链接
检查url链接是否已经有参数的php代码 添加 ? 或 &
PHP生成网页快照 不用COM不用扩展.
一步一步学习PHP(1) php开发环境配置
一步一步学习PHP(2):PHP类型
一步一步学习PHP(3) php 函数
一步一步学习PHP(4) php 函数 补充2
提高PHP编程效率 引入缓存机制提升性能
php 数组的合并、拆分、区别取值函数集
PHP采集相关教程之一 CURL函数库
IP138 IP地址查询小偷实现代码
php 生成静态页面的办法与实现代码详细版
一步一步学习PHP(5) 类和对象
一步一步学习PHP(6) 面向对象
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
PHP 截取字符串函数整理(支持gb2312和utf-8)
php foreach 使用&(与运算符)引用赋值要注意的问题
PHP IPV6正则表达式验证代码
用PHP ob_start()控制浏览器cache、生成html实现代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 50 ::
收藏到网摘: n/a

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