What is the best way to count page views in PHP/MySQL?

后端 未结 8 761
[愿得一人]
[愿得一人] 2020-12-07 09:47

And by best I mean most efficient, right now placing this on my post.php file is the only thing I can think of:

$query = mysql_query(\" UPDATE posts SET view         


        
8条回答
  •  轮回少年
    2020-12-07 10:32

    If you're interested in conserving resources and still using SQL for reporting, and precise # doesn't matter, you could try sampling like this (modify sample rate to suit your scale):

    $sample_rate = 100;
    if(mt_rand(1,$sample_rate) == 1) {
        $query = mysql_query(" UPDATE posts SET views = views + {$sample_rate} WHERE id = '{$id}' ");
        // execute query, etc
    }
    

提交回复
热议问题