Forum Moderators: coopster

Message Too Old, No Replies

Is this the right way to increment multiple columns?

If so, why doesn't it work.

         

HughMungus

4:33 am on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This looks to me as if it should work to display a list of items in a database, then update a field named "views" for each one (to record the number of views/day or whatever).

Edit: I just found out that I had a typo. But the question remains: is this the "right" way?

while ($row = mysql_fetch_array($sql_whatever))
{
if ($row[type] == "Something")
{
echo ''.$row[pretext].'<A HREF="/?a='.$row[id].'">'.$row[anchor].$row[tracker].'</A> <BR><BR>';
$sql_views="UPDATE aff SET views = views + 1 WHERE id = '$row[id]'";
if(!mysql_query($sql_views)) die(mysql_error());

}
}

hughie

12:20 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



I would say that is a decent way to do it.
bear in mind that Mysql is a lot faster fetching from the database than updating it. If you have to do a lot of
"$sql_views="UPDATE aff SET views = views + 1 WHERE id = '$row[id]'"; "
in each page (and your traffic is high) you could look at re-building it to put less stress on the server.

Not quite sure how to though ;-)

hughie

dmorison

12:26 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would probably make more sense to do the views increment in one go, using the same WHERE clause as you used to select the original products to view.

For example:


$where = "widget_type='BLUE'";

$sql = "SELECT * FROM aff WHERE $where";

// execute $sql and display results as per your code, and then:

$sql = "UPDATE aff SET views = views + 1 WHERE $where";

// execute update SQL

This way, you're only doing 2 hits on the database instead of (n+1) where n is the number of products returned.