Lamez Posted March 17, 2008 Share Posted March 17, 2008 I want a loop that will take ever user, and write a 0 in the paid column. I wanna make sure I got this down, so here is what I have: <?php if ($paid == ($_POST['paid'])){ $results = mysql_query("SELECT `username`, `paid` FROM `users`") while($row = mysql_fetch_array($result)){ $user = $row['username']; mysql_query("UPDATE `users` SET `paid` = '0' WHERE `username` = '$user'"); } } ?> would this work? Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/ Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 you can update multiple rows at a time the WHERE portion dictates what is updated i.e Update `users` SET `paid` = '0' will update all rows in users table to have paid = 0 Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494049 Share on other sites More sharing options...
Lamez Posted March 17, 2008 Author Share Posted March 17, 2008 isn't that what I have? mysql_query("UPDATE `users` SET `paid` = '0' WHERE `username` = '$user'"); Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494058 Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 except you are doing a lot of queries I am doing 1 Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494062 Share on other sites More sharing options...
Lamez Posted March 17, 2008 Author Share Posted March 17, 2008 oh I understand now, so instead of doing this: mysql_query("UPDATE `users` SET `paid` = '0' WHERE `username` = '$user'"); I could do this w/o a loop?: mysql_query("UPDATE `users` SET `paid` = '0'"); Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494067 Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 Since you do not have a where clause in your first query you are essentially updating every row. Which is why cooldude is giving you a simpler solution if that's what you want to do. now if you only want to update certain users then your code would be fine when you add a where clause in there. <?php if ($paid == ($_POST['paid'])){ $results = mysql_query("SELECT `username`, `paid` FROM `users` WHERE `users` IN ('John', 'Jack', 'Harry')") while($row = mysql_fetch_array($result)){ $user = $row['username']; mysql_query("UPDATE `users` SET `paid` = '0' WHERE `username` = '$user'"); } } ?> Now just 3 rows will be updated Ray Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494068 Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 That makes no sense craygo because you could just say Update `users` set paid='0' where username IN ('John', 'Jack', 'Harry') You shouldn't need to query a table to figure out the rows to update unless you have some fancy thing to it. Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494074 Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 Well it does make sense cause they both will work. I know what you mean by using 2 queries makes no sense, I just wanted to point out the use of the where part of the query. I think I just got stuck on using his existing code to point it out. Ray Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494081 Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 Well it does make sense cause they both will work. I know what you mean by using 2 queries makes no sense, I just wanted to point out the use of the where part of the query. I think I just got stuck on using his existing code to point it out. Ray I'm all about running a query to the extent of mysql knowledge because it usually simplifies stuff Quote Link to comment https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494247 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.