ldoozer Posted January 28, 2008 Share Posted January 28, 2008 I have a database with lots of tables and many tables have a field called requests which hold how many times a page for instance has been requested. Here the thing - now that the website has been tested, i want to reset all the stats to 0. Can i do this in one sql statement? Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/ Share on other sites More sharing options...
cooldude832 Posted January 28, 2008 Share Posted January 28, 2008 why would you have so many tables with respect to a single data type (number of page request?) Should be a single table and then you could easily just write. <?php $q = "Update `Reqest_Count_table` Set Hit_Count = '0'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); ?> That would update all counts to 0 in that table Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451293 Share on other sites More sharing options...
ldoozer Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks for the reply. not sure why that is the case - i did not create the db, I'm just a project manager trying to get by Is it not possible to update all at once? Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451299 Share on other sites More sharing options...
cooldude832 Posted January 28, 2008 Share Posted January 28, 2008 if the field name is consitent across the database say <?php $tables = array("table1", "Table2"); $update_field = "Hit_Cont"; foreach($tables as $value){ $q = "Update `".$value."` Set ".$update_field."='0'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } ?> Should work. Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451302 Share on other sites More sharing options...
ldoozer Posted January 28, 2008 Author Share Posted January 28, 2008 thinking about it - the tables that have this field are separated in to blogs, forum topics, news, pages, products. So might be in different tables for different areas of the site? Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451304 Share on other sites More sharing options...
cooldude832 Posted January 28, 2008 Share Posted January 28, 2008 it would be smarter to categorize the pages in a single page with another field. Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451305 Share on other sites More sharing options...
ldoozer Posted January 28, 2008 Author Share Posted January 28, 2008 Thank you will try that php script now, and will backup the db first Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451306 Share on other sites More sharing options...
tgavin Posted January 28, 2008 Share Posted January 28, 2008 If the columns have a default value of 0, couldn't you just use $sql = mysql_query("TRUNCATE TABLE table_name"); Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451313 Share on other sites More sharing options...
ldoozer Posted January 28, 2008 Author Share Posted January 28, 2008 just realised that I dont have the class that connects to the db. need to do this: $update_field = "Hit_Cont"; foreach($tables as $value){ $q = "Update `".$value."` Set ".$update_field."='0'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } ?> but in sql? Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451326 Share on other sites More sharing options...
cooldude832 Posted January 28, 2008 Share Posted January 28, 2008 If the columns have a default value of 0, couldn't you just use $sql = mysql_query("TRUNCATE TABLE table_name"); No! Truncate will empty all records out of a table Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451380 Share on other sites More sharing options...
tgavin Posted January 28, 2008 Share Posted January 28, 2008 If the columns have a default value of 0, couldn't you just use $sql = mysql_query("TRUNCATE TABLE table_name"); No! Truncate will empty all records out of a table Right. he said he wanted to reset all to zero. I take that as meaning 'starting from scratch'. If each column has a default value of 0, then he will achieve his goal. Otherwise, why reset to zero? Quote Link to comment https://forums.phpfreaks.com/topic/88200-resetting-stats/#findComment-451462 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.