Page 1 of 1

Error with my code....

Posted: Sun Oct 21, 2007 1:48 pm
by SirChick
Im getting an error with this code, not sure what i got to do to fix it:

Code: Select all

If ($Result == 'B'){

//Get the users
$get_users = mysql_query("SELECT * FROM userresgistration WHERE CountryID='1'");

//Loop through all the records
while($user = mysql_fetch_array($get_users)) {

  //Do something which has a 20% chance of succeeding.
  //Choose a random number between 1 and 5
  $rand = rand(1,1);

  //If the number is 3 (which there is a 20% chance of it being) update the user
  if($rand == 1) {
    $update_user = "UPDATE userregistration SET IllnessType='test' WHERE CountryID='1'";
			$result = mysql_query($update_user) or die(mysql_error());
			}

}
Error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test.php on line 142

$rand = rand(1,1); is only cos i need it to be 100% of the time for testing.. just encase u wondered

Posted: Sun Oct 21, 2007 2:06 pm
by Josh1billion
Change this line..

Code: Select all

$get_users = mysql_query("SELECT * FROM userresgistration WHERE CountryID='1'");
to

Code: Select all

$get_users = mysql_query("SELECT * FROM userresgistration WHERE CountryID='1'"); 
if (!$get_users)
    die("Error: " . mysql_error());
Then post what the error message says.

Posted: Sun Oct 21, 2007 3:03 pm
by SirChick
I had a spelling mistake for table name but though thats fixed its still not working

Ok no error message occurs now.. but it should be working every time as i put the rand as 1 to 1 =/

Code: Select all

If ($Result == 'B'){

//Get the users
$get_users = mysql_query("SELECT * FROM userregistration WHERE CountryID='1' AND IllnessType='0'");
if (!$get_users)
    die("Error: " . mysql_error());

//Loop through all the records
while($user = mysql_fetch_array($get_users)) {

  //Do something which has a 20% chance of succeeding.
  //Choose a random number between 1 and 5
  $rand = rand(1,1);

  //If the number is 3 (which there is a 20% chance of it being) update the user
  if($rand == 1) {
    $update_user = "UPDATE userregistration SET IllnessType='test' And TimeOfIllness='$Date' WHERE CountryID='1'";
			$result = mysql_query($update_user) or die(mysql_error());
			}

}

}
encase you need to know $Date is $Date = date("Y-m-d H:i:s",time());

Posted: Sun Oct 21, 2007 4:41 pm
by gregsometimes
Explain what you're trying to do here:

$rand = rand(1,1); // this will always return 1, then why is it a condition?

Are you generating a (whole) random number between 1 and 1, which is always 1?

Posted: Sun Oct 21, 2007 5:15 pm
by SirChick
i put it as 1:1 to test it works as it will work 100% of the time so itll save me refreshing non stop till eventually it hits the number.... but the update wont work :

I have this:

Code: Select all

If ($Result == 'B'){

//Get the users
$get_users = mysql_query("SELECT * FROM userregistration WHERE CountryID='1' AND IllnessType='0'");
if (!$get_users)
    die("Error: " . mysql_error());

//Loop through all the records
while($user = mysql_fetch_array($get_users)) {

  //Do something which has a 20% chance of succeeding.
  //Choose a random number between 1 and 5
  $rand = rand(1,1);

  //If the number is 3 (which there is a 20% chance of it being) update the user
  if($rand == 1) {
    $update_user = "UPDATE userregistration SET IllnessType='Winter Flu' And TimeOfIllness='$Date' WHERE CountryID='1'";
			$result = mysql_query($update_user) or die(mysql_error());
    echo $update_user, '<br>';
	}

}

}
The echo produces:
UPDATE userregistration SET IllnessType='Winter Flu' And TimeOfIllness='2007-10-21 23:15:54' WHERE CountryID='1'
UPDATE userregistration SET IllnessType='Winter Flu' And TimeOfIllness='2007-10-21 23:15:54' WHERE CountryID='1'
UPDATE userregistration SET IllnessType='Winter Flu' And TimeOfIllness='2007-10-21 23:15:54' WHERE CountryID='1'

Yet in my database the fields for the 3 users that went into the while loop did not update... once the update is working ill set rand back to 1:5 which then makes it only 20% of the time.

Posted: Sun Oct 21, 2007 5:42 pm
by gregsometimes
can you replace this:

Code: Select all

$update_user = "";
$result = mysql_query($update_user) or die(mysql_error());
echo $update_user, '<br>';
with:

Code: Select all

if (($q = mysql_query("UPDATE userregistration SET IllnessType='Winter Flu' And TimeOfIllness='$Date' WHERE CountryID='1'")) != FALSE)
  print "update successful...<br>";
else
  print mysql_error();
and see if you get 3 updates or an error message? let me know what you get, then I might be able to help

Posted: Sun Oct 21, 2007 5:53 pm
by SirChick
update successful...
update successful...
update successful...

still blank fields in the Database how ever...

Posted: Sun Oct 21, 2007 6:02 pm
by gregsometimes
Are the rows originally empty?

Don't forget that an UPDATE query will only "update" ___already existing___ rows. Otherwise use INSERT.

It makes sense to me. The query is successful, because there is nothing to update. Correct me if I'm wrong.

If this isn't the case though, the problem is with something else, not the query.

Posted: Sun Oct 21, 2007 6:04 pm
by SirChick
there are 3 users with country ID = 1....

also surely if the while looped 3 times then there must be 3 existing rows...hense the 3 echo's .. right :S ?

the two fields updating which are type of illness and time of illness are "0" and " 0000-00-00 00:00:00" prior to update.. what should they be by default if that wont work ?

Posted: Sun Oct 21, 2007 6:55 pm
by gregsometimes
a-ha!

update syntax:

Code: Select all

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]


// so, replace the query line to:

"UPDATE userregistration SET IllnessType='Winter Flu', TimeOfIllness='$Date' WHERE CountryID='1'"

// (replace And with a comma)
If this doesn't work. It could be the $Date var, otherwise I'm out of ideas.

Posted: Sun Oct 21, 2007 7:27 pm
by SirChick
gregsometimes wrote:a-ha!

update syntax:

Code: Select all

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]


// so, replace the query line to:

"UPDATE userregistration SET IllnessType='Winter Flu', TimeOfIllness='$Date' WHERE CountryID='1'"

// (replace And with a comma)
If this doesn't work. It could be the $Date var, otherwise I'm out of ideas.

excellent so how come AND works for me on other queries but not this one...


Gah it worked but now it won't work after the first try... this is what i got (with the rand back to normal)

Code: Select all

$get_users = mysql_query("SELECT * FROM userregistration WHERE CountryID='1' AND IllnessType='0'");
if (!$get_users)
    die("Error: " . mysql_error());

//Loop through all the records
while($user = mysql_fetch_array($get_users)) {

  //Do something which has a 20% chance of succeeding.
  //Choose a random number between 1 and 5
  $rand = rand(1,5);

  //If the number is 3 (which there is a 20% chance of it being) update the user
  if($rand == 3) {
$Date = date("Y-m-d H:i:s",time());
$q = mysql_query("UPDATE userregistration SET IllnessType='Winter Flu', TimeOfIllness='$Date' WHERE CountryID='1'");
    if ($q)
 {
  print "update successful...<br>";
 }
else
 {
  print mysql_error(); 
 }
	}

}
can you see a mistake :S


print mysql_error(); this does not happen for some reason.