Jump to content

[SOLVED] row accessed count


phpocean

Recommended Posts

Hello beautiful people,

 

What I am looking for is a code to count and display the number of times a row has been accessed.

 

Can anyone point me in the right direction?

 

I have done a few google but it seen I can only find count for whole page and not individual row.

 

Thank for any help.

 

Ocean

Link to comment
https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/
Share on other sites

I can create a new column no problem.

What function or code would I need to achieve so that mysql would update the column everytime it's accessed?

 

If it required long code, I can just go google it up, but if it's not long code can you show me how?

 

Ken, do you know how to do what y2yang say? Updating the column every time it's accessed using php.

I don't know any other way of doing it so this is the only way I know how. To me, there are 2 things that actually access a row - UPDATE and SELECT SQLs. So just add a count column to each table. :-\

 

UPDATE is easy. You just run the UPDATE query adding the count field in.

 

Example -

UPDATE tablename SET blah='$blah', count=count+1 WHERE id=4

 

 

SELECT is harder. You have to select the columns + ID. Make sure you always select some unique key. Then after SELECT, you have to run a while loop to UPDATE.

 

$result = mysql_query('SELECT id, prices FROM tablename WHERE prices > 40;');
if (!$result) exit;
$errors = array();
while ($row = mysql_fetch_assoc($result)) {
     $update = mysql_query('UPDATE tablename SET count=count+1 WHERE id='.$row['id']);
     if (!$update) $errors[] = $row['id'];
}

Something like that. The $errors array is to store any errors on keys that failed the query. Just for reference really, but none should fail.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.