phpocean Posted May 26, 2009 Share Posted May 26, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/ Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 Define accessed. As in every time the row is selected or updated? You can have a new column for accessed to display the number of how many times the row has been accessed, but you'll need to update that column yourself. Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842022 Share on other sites More sharing options...
y2yang Posted May 26, 2009 Share Posted May 26, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842050 Share on other sites More sharing options...
phpocean Posted May 26, 2009 Author Share Posted May 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842073 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842076 Share on other sites More sharing options...
phpocean Posted May 26, 2009 Author Share Posted May 26, 2009 Thank Ken. I only used this part of the code and it seen to do the trick beautifully but I don't know if using it this way posses any threat to my database. <? mysql_query("UPDATE news SET hit=(hit + 1) WHERE id = '".$Article."'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842514 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 Well depends. You should cast $Article as an int if you haven't already. Quote Link to comment https://forums.phpfreaks.com/topic/159636-solved-row-accessed-count/#findComment-842533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.