PHP& MySQL为动态页面计算页面浏览量的最佳方法 [英] PHP & MySQL best way to count page views for dynamic pages

查看:76
本文介绍了PHP& MySQL为动态页面计算页面浏览量的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为动态网页(例如下面的url示例)计算网页浏览量的最佳方法是什么?我正在使用PHP和MySQL.简短的解释会有所帮助.谢谢!

What is the best way to count page views for dynamic pages like the url example below? I'm using PHP and MySQL. A brief explanation would help. Thanks!

http://www.example.com/posts/post.php? id = 3

推荐答案

通常,表结构如下所示:

Usually the table structure looks like this:

表格页:

id | name            | ... 
==========================
1    Some Page
2    Some Other Page

表pages_views:

table pages_views:

page_id | views
================
1         1234
2         80

pages_views在page_id

where pages_views has a unique index on page_id

用于增加视图的MySQL语句如下所示:

The MySQL statement to increment the views then looks as follows:

INSERT INTO `pages_views` SET views=1 WHERE page_id=?
    ON DUPLICATE KEY UPDATE views=views+1 ;

由于pages_views.page_id是唯一的,因此如果页面的行不存在,则会创建该行;如果存在(即重复键"子句),则计数器将递增.

Since pages_views.page_id is unique, the row for the page will get created if it doesn't exist; if it exists (that's the "duplicate key" clause), the counter will be incremented.

我在这里选择了两个单独的表,因为CMS页通常不经常更新(因此,它们的负载大部分是读取的),而每个视图的页面视图都可以读取和更新.

I chose two separate tables here, as CMS pages usually aren't updated too often (and therefore, their load is mostly reads), whereas page views are read and updated, well, with each page view.

这篇关于PHP& MySQL为动态页面计算页面浏览量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆