Page 1 of 1

Fetch variable not working as anticipated

Posted: Wed Jul 21, 2010 10:33 pm
by Smudly
In my code, I loop through 3 random users, and I am trying to add 500 to their total number of credits. The variable $credits isn't being captured correctly, as it shows blank when I try to echo it. The odd part however, is $userid (primary key) is captured into the variable. What do I need to modify in my code to fix this issue?
Or, perhaps is there a way to just add 500 to the current total of credits by using the mysql update?

Code: Select all

<?php

include_once('../inc/connect.php');

$date = date("Y-m-d");

$drawquery = mysql_query("SELECT id FROM userstats WHERE `todaysurfed`>=250 AND DATEDIFF(NOW(),`wondaily`) > 30 OR `wondaily`='0000-00-00' ORDER BY rand() LIMIT 3");

while($row = mysql_fetch_array($drawquery))
  {

$userid = $row['id'];
$credits = $row['credits'];
$addcredits = 500;
$newcredits = $credits + $addcredits;

echo $userid."<br />";

$updatewinners = mysql_query("UPDATE userstats SET wondaily='$date', credits='$newcredits' WHERE id='$userid'");

}

?>


Thanks

Re: Fetch variable not working as anticipated

Posted: Wed Jul 21, 2010 10:49 pm
by whizzopia
Your query only selects 'id'. You should include 'credits' in the select query.

Re: Fetch variable not working as anticipated

Posted: Wed Jul 21, 2010 11:03 pm
by John Cartwright
You don't need to select the users if you don't need their information or other purposes. To simply add 500 to 3 random rows, simply do

Code: Select all

   UPDATE userstats 
   SET credits = credits + 500, 
          wondaily = NOW() 
   WHERE `todaysurfed`>=250 
       AND DATEDIFF(NOW(),`wondaily`) > 30 
         OR `wondaily`='0000-00-00' 
   ORDER BY rand() 
   LIMIT 3