Error in php script when cycling

Can’t figure out why it is doing this - probably something really stupid, but…

[php]
$todaydate = $_SERVER[‘REQUEST_TIME’]; //current timestamp
//Check all active members
$db = mysqli_connect($db_hostname,$db_username,$db_password,“paratb_members”) or die (“Cannot Connect to database”);
$result = mysqli_query($db,“SELECT * FROM members WHERE active = ‘y’ AND exempt = ‘n’”) or die (“Cannot select from db”);
while ($row = mysqli_fetch_array($result)) {
extract($row);
if ($expiredate < $todaydate){
$query = “UPDATE members SET active=‘n’ WHERE (id = ‘$id’)”;
$result = mysqli_query($db,$query) or die(“A fatal MySQL error occured in the Member Database.\n
Error: (” . mysqli_errno() . ") " . mysqli_error());
echo “User $fname $lname ($id) active status changed to n
”;
unset($fname, $lname, $email, $id, $expiredate, $whendue);
}
}
[/php]

After performing the first function (e.g. User xxx xxx (138) active status changed to n) gives me this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/paratb/public_html/admin/cron/duesreminder.php on line 68

line 68 is simply [php]while ($row = mysqli_fetch_array($result)) {[/php]

Any help would be greatly appreciated

Thanx

I’m a PDO person so I am not going to be much help, but I don’t know why you’re doing extract($row)? And you are not taking full advantage of mysqli statements.

why you're doing extract($row)? And you are not taking full advantage of mysqli statements.

Because I am a Noobie and do not know any better. Thus, your response was not very helpful

If $result is a boolean, that means the query failed and is returning false. That is the advantage of real error handling, compared to die statements.

But why is it failing after the first result and action?

I can’t tell you that, that is why exception handling is important.

[php]try {

// database stuff

} catch (Exception $e ) {
echo $e->getMessage();
}[/php]

Probably me but I am gettiung a syntax error inserting the catch…

Interestingly if I recove the query and just echo, it does list all the members toi change. With query, it changes first one and then gives the error

What is the actual code you have?

The query within the loop is also changing the outside values. Name chamge is likely in order.

The entire code is what I listed at the beginning of thios thread but here it is again

[php]

<? require_once ('./connect.php'); // INACTIVATE EXPIRED MEMBERS $todaydate = $_SERVER['REQUEST_TIME']; //current timestamp //Check all active members $db = mysqli_connect($db_hostname,$db_username,$db_password,"paratb_members") or die ("Cannot Connect to database"); $result = mysqli_query($db,"SELECT * FROM test WHERE active = 'y' AND exempt = 'n'") or die ("Cannot select from db"); while ($row = mysqli_fetch_array($result)) { extract($row); if ($expiredate < $todaydate){ $query = "UPDATE test SET active='n' WHERE (id = '$id')"; $result = mysqli_query($db,$query) or die("A fatal MySQL error occured in the Member Database.\n
Error: (" . mysqli_errno() . ") " . mysqli_error()); echo "User $fname $lname ($id) active status changed to n
"; unset($fname, $lname, $email, $id, $expiredate, $whendue); } } echo "FINISHED"; ?>

[/php]

As I said originally, it will make the first change (changes active to n) but then go into the error

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/paratb/public_html/admin/cron/duesreminder.php on line 68

If I comment out the $query and $result commands, it will give a compete list of all those in db that should be changed to “n” so the script does work. It is just AFTER it makes the first change in the query, it then errors

So, check this out:

	[php]$result = mysqli_query($db,"SELECT * FROM test WHERE active = 'y' AND exempt = 'n'") or die ("Cannot select from db");
	while ($row = mysqli_fetch_array($result)) {
if ($expiredate < $todaydate){
			$query = "UPDATE test SET active='n'	WHERE (id = '$id')";
	$result = mysqli_query($db,$query) or die("<b>A fatal MySQL error occured in the Member Database</b>.\n<br>Error: (" . mysqli_errno() . ") " . mysqli_error());  
		}
	}[/php]

Your while loop is running based on $row getting values from $result, right?
Well, within that same loop you change the value of $result.

Your whole logic is wrong. Stop running queries in a loop. That is the first red flag you are doing it wrong. And stop using extract. You only need ONE query. And stop displaying system errors to the user. That info is only good for a coder or a hacker.

  • I see from your other posts on this and other forums you are just not listening to what you are told. If your not going to follow the expert advice given to you then you are just wasting everyone’s time.

Post or PM an sql dump of your DB and I will show you how to do it right. The code you posted is junk.

Sponsor our Newsletter | Privacy Policy | Terms of Service