PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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
Last edited by SirChick on Sun Oct 21, 2007 3:03 pm, edited 1 time in total.
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());
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 :
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.
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
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 ?
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.
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)
$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.