Updating message views every time a message is viewed using php

I do not know if this is allowed, but I need to clarify the solution to the question.

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

I have an exact question. I just don't know how the solution makes sense, here is the solution:

$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
}

Any help?

+3
source share
2 answers

Here mt_rand () generates a random number from 1 to 100, so the probability that this number will be equal to one is 1/100. If this generates 1, we increase the value of views by 100. Thus, an effective increase in the database per view = (Probability of increasing vision) * (increase in the database) = (1/100) * 100 = 1

, 1 .

. MySQL , PHP rand.

+3

, , 1 100 ($ sample_rate). 1, ( ).

, . , -.

, , , .

+1

All Articles