Jump to content

MySql Loop, How?


Lamez

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/96541-mysql-loop-how/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494068
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/96541-mysql-loop-how/#findComment-494247
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.