Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.