andre3 Posted December 27, 2008 Share Posted December 27, 2008 Hi guys, whats the easiest way to update many different rows inside a table with one query, for example; i have a table called site_pages with id, name, position and there are 4 rows in there with is 1, 2 , 3 , 4, and i need to update all row id + 1.. how wud i do that? i tried using a while loop with a update statement inside it, but it update all the rows with the same number.... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/ Share on other sites More sharing options...
revraz Posted December 27, 2008 Share Posted December 27, 2008 Post what you tried. Sounds like your variable was empty. Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724201 Share on other sites More sharing options...
andre3 Posted December 27, 2008 Author Share Posted December 27, 2008 here is it: $sql = mysql_query("SELECT * FROM site_pages"); while($row = mysql_fetch_array($sql)) { $newid = $row[id] + 1; mysql_query("UPDATE site_pages SET id='".$newid."'"); } Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724210 Share on other sites More sharing options...
hobeau Posted December 27, 2008 Share Posted December 27, 2008 Instead of doing a SELECT statement and then iterating through it with a loop, simply run the following sql command: mysql_query("UPDATE site_pages SET id = (id + 1)"); If this is a primary key column, you may have to alter the table so that it is not a primary key temporarily while you run this query as it may produce duplicate keys thereby violating the primary key unique rule. Hope this helps!!! Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724280 Share on other sites More sharing options...
ToonMariner Posted December 27, 2008 Share Posted December 27, 2008 NEVER executre queries inside a loop - its the most horrible thing - simply highlights the lack of thought that has gone into someones 'code'. Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724286 Share on other sites More sharing options...
hobeau Posted December 27, 2008 Share Posted December 27, 2008 NEVER executre queries inside a loop - its the most horrible thing - simply highlights the lack of thought that has gone into someones 'code'. well,, maybe not 'never',, there are some rare occasions, but for the most part,, ya your right ToonMariner lol. Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724298 Share on other sites More sharing options...
ToonMariner Posted December 27, 2008 Share Posted December 27, 2008 NOPE I'll stick with never Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-724310 Share on other sites More sharing options...
andre3 Posted January 4, 2009 Author Share Posted January 4, 2009 still doesnt work accurate an am still trying, any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-729012 Share on other sites More sharing options...
andre3 Posted January 4, 2009 Author Share Posted January 4, 2009 i think i am getting somewher now, one question, is there a way that i can change the id value thats into the bracked it a variable? reason is that when i run the script its updating all id numbrs uniquely but it updates them with the last id number example, if the last id number is 100 it adds that number to ever other id when the script runs, an i just need it to add a extra 1 to each id number each time. example; ($originalposition + 1) mysql_query("UPDATE site_pages SET id = (id + 1)"); ? Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-729225 Share on other sites More sharing options...
andre3 Posted January 7, 2009 Author Share Posted January 7, 2009 i got it to work thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/138507-how-can-i-update-many-different-rows/#findComment-731249 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.