PHP Count 站点视图每天创建一行 [英] PHP Count site view creating a row every day

查看:37
本文介绍了PHP Count 站点视图每天创建一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 1: 我正在尝试制作一个可以计算页面浏览量(也在刷新时)并每天在 db 上创建新行的脚本,我将在我的管理仪表板中进行计数.

QUESTION 1: I'm trying to make a script that can count the page view ( on refresh too ) and make a new row on db every day, the I will count in my admin dashboard.

这是我的代码:

$today = date("d-m-Y");
$siteViewsPrintSQL = $DB_CON -> query("SELECT * FROM statistics");
$siteViewsPrint = $siteViewsPrintSQL -> fetch(); 

if ($siteViewsPrint['date'] == $today) {
    $updateSiteViewsCount = "UPDATE andreaem.statistics SET site_views = site_views+1 WHERE date = $today";
    $DB_CON ->query($updateSiteViewsCount);
} elseif ($siteViewsPrint['date'] != $today) {
    $createSiteViewsCount = "INSERT INTO `andreaem`.`statistics` (`ID`, `date`, `site_views`, `new_users`) VALUES (NULL, '$today', '0', '0');";
    $DB_CON -> query($createSiteViewsCount);
    $updateSiteViewsCount = "UPDATE andreaem.statistics SET site_views = site_views+1 WHERE date = $today";
    $DB_CON -> query($updateSiteViewsCount);
} else {
    print 'Error while updating siteviews.';
}

我知道这可能不是正确的做法(接受任何建议),这会导致每次刷新都会创建一个新行,而不是更新现有行.

I know maybe isn't the correct way to do ( any suggestion accepted ) and this cause every refresh a new row is created instead of update existing one.

问题 2: 在 db 中保存这些值后,我必须从表中获取并在我的仪表板中打印,所以我使用 PDO 连接和 array_sum(函数 convert_to_unit 和 bd_nice_number convert如果是 1000 或 M 如果是 1000000)

QUESTION 2: After saving this values in db, i must to fetch from the table and print in my dashboard, so I'm using a PDO connection and an array_sum (functions convert_to_unit and bd_nice_number convert the number in n K if is 1000 or M if is 1000000)

$siteViewsPrintSQL = $DB_CON -> query("SELECT site_views FROM statistics");
$siteViewsPrint = $siteViewsPrintSQL -> fetchAll(PDO::FETCH_ASSOC);
$siteViewsPrintResult = convert_to_unit(bd_nice_number(array_sum($siteViewPrint)));

这返回 0 而不是总和.

This return 0 instead of the sum.

感谢所有可以提供帮助的人!

Thanks to all who can help!

推荐答案

是的,坦白说,这段代码和正确的代码正好相反.

Yes, frankly, this code is just directly opposite to a correct one.

由于您显然刚刚开始学习数据库,我强烈建议您采用最基本的方法,即存储每个匹配项.它将大大缩短您的代码,并让您学习基本的数据库功能.

As you are apparently just started learning databases, I would strongly suggest you to go the most basic way, which will be storing every hit. It will make your code dramatically shorter and will let you to learn basic database functions.

所以让你的桌子像

dt datetime,
ip varchar(15),

然后在每次点击时运行这样的查询

and then on every hit run a query like this

$stmt = $DB_CON->prepare("INSERT INTO stats VALUES (NOW(),?)");
$stmt->execute([$_SERVER['REMOTE_ADDR']]);

要获得计数,您必须运行此代码

and to get the count you will have to run this code

$count = $DB_CON->query("SELECT count(*) FROM stats")->fetchColumn();

最后一个查询是最重要的:如您所见,数据库可以为您计数(以及求和、计数平均值或进行任何其他计算).所以你永远不应该在 PHP 端做任何计算 bu 总是从数据库请求最终结果.

This last query is most important: as you can see, a database can count (as well as sum, count averages or do whatever else calculations) for you. So you should never ever do any calculations on the PHP side bu always request the final result from database.

通过此数据库设置,您可以通过简单地以这种方式对结果进行分组来获得每日流量:

With this database setup you will be able to get your daily traffic, by simply grouping results this way:

$daily = $DB_CON->query("SELECT count(*), date(dt) FROM stats GROUP BY date(dt)")->fetchAll();

甚至还可以吸引新访客.

And can even get new visitors as well.

这篇关于PHP Count 站点视图每天创建一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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