MortimerJazz Posted November 16, 2006 Share Posted November 16, 2006 Ok, I think I've sort of got the hang of this. I've inserted a "count" field into my table which I'm planning on incrementing by 1 every time the data is displayed.My question is, how do I set up two SQL queries to (a)Extract the necessary information to display on the screen and (b) Update the "count" filed. Presumably the UPDATE command has to be inserted into the mysql_fetch_array so that each record gets updated should there be more than oneAt the moment I've got this:[code]$recent_sql = mysql_query("SELECT * FROM toys WHERE id='$id' ORDER BY price DESC"); while ($recent = mysql_fetch_array($recent_sql)) { $username=$recent['username']; $colour=$recent['colour']; $count=$recent['count']; $count++;$update = mysql_query("UPDATE toys SET count=$count WHERE id=$id") or die(mysql_error()); }[/code]Will that actually work?Thanks for your help, Link to comment https://forums.phpfreaks.com/topic/27435-counting-the-number-of-times-that-a-record-is-viewed-nested-sql-commands/ Share on other sites More sharing options...
printf Posted November 16, 2006 Share Posted November 16, 2006 Don't do the update in the result loop, wait until you exit the loop then run a single query to update all the rows with (1) query, using the auto increment primary key![code]$update = array ();$recent_sql = mysql_query ( "SELECT * FROM toys WHERE id = '" . $id . "' ORDER BY price DESC" ); while ( $recent = mysql_fetch_array ( $recent_sql ) ){ $username = $recent['username']; $colour = $recent['colour']; $count = $recent['count']; // toys should ha a primary key (auto_increment) $update[] = $recent['toy_id'];}mysql_query ( "UPDATE toys SET count = ( count + 1 ) WHERE toy_id IN(" . implode ( ', ', $update ) . ")" ) or die ( mysql_error () );[/code] Link to comment https://forums.phpfreaks.com/topic/27435-counting-the-number-of-times-that-a-record-is-viewed-nested-sql-commands/#findComment-125478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.