mysqli_fetch_array() error

Don’t understand why I am getting 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 9

[php]1 require_once (’…/connect.php’);
2 $todaydate = $_SERVER[‘REQUEST_TIME’]; //current timestamp
3 $db = mysqli_connect($db_hostname,$db_username,$db_password,“paratb_members”) or die (“Cannot Connect to database”);
4 $result = mysqli_query($db,“SELECT * FROM members WHERE active = ‘y’ && exempt = ‘n’”) or die (mysqli_error());
5 while ($row = mysqli_fetch_array($result)) {
6 extract($row);
7 if ($expiredate < $todaydate){
8 $query = “UPDATE members SET active=‘n’ WHERE (id = ‘$id’)”;
9 $result = mysqli_query($db,$query) or die (mysqli_error()); ;
10 echo “User $fname $lname ($id) active status changed to n
”;
11 unset($id);
12 }
13 }
[/php]

The script actually does what it is supposed to but throws the error after every change it makes. NO mysqli error is defined. Funny thing is that if I comment out line 9, it cycles through without error so it is definately the successful query that causes the error?

Any suggestions?

Thanx

Asked and answered on another forum.

Yeah but the reply was not very helpful for a N00b and follow-ups were nbot answered. That why peeps post to more than 1 forum in hope that SONONE will give a meanbinful and helpful response that will assist in solving the problem

If the answer was unclear to you then why didn’t you ask to poster to kindly clarify what he/she meant? I’m sure there would had been more replies.

why didn't you ask to poster to kindly clarify what he/she meant?

What I previously wrote:

and follow-ups were not answered
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

Means, the query returned false.
[php]
if ( !$result ) { // checks if $result == false
// code to deal with failure
}[/php]

I understand that it is returning false, what I don’t understand is why and how to fix it

Are ypu suggesting adding that snippet so that no error (false) is expressed.
Not sure that answers the question/problem because if there are no 'y" to change to “n” in the query, there is no error. It only happens AFTER a query is executed.

mysqli_query explains why the error is occurring.

If you use MYSQLI_USE_RESULT all subsequent calls will return error Commands out of sync unless you call mysqli_free_result()

Are you suggesting that I add " mysqli_free_result($result);", i.e., :

[php]
8 $query = “UPDATE members SET active=‘n’ WHERE (id = ‘$id’)”;
9 $result = mysqli_query($db,$query) or die (mysqli_error()); ;
10 echo “User $fname $lname ($id) active status changed to n
”;
11 mysqli_free_result($result);
12 }
13 }
[/php]

I am actually suggesting you use PDO and prepared statements. Then you don’t have to worry about mysql oddities like this.

I agree and in time I will learn PDO but ATM I am just trying to get this script to work without error.
BTW, mysqli_free_result($result); just causes another error…

Possibly you could check for an error. Right now you are not, at least, properly.

Your code
[php] or die (mysqli_error())[/php]

Manual
[php]string mysqli_error ( mysqli $link )[/php]

As well, you should change your die() statements to trigger_error(), so that your code would be portable to production. With die() you cannot suppress printing the contents to the screen. Where trigger_error() will be handled by PHP’s native error handling.

Gee thanx but nobody has yet helped me solve the error other than convert to PDO :’(

And no matter what I try, I do not get a specific error.

We have tried, but you aren’t listening. The query is failing. Since we are not sitting, and looking over your shoulder, we cannot tell you why that query is failing. It looks ok, but you could have a misspelled column name, or gotten it wrong altogether. Properly checking why the query is failing is the only way.

To do that, you must properly use the error functions. This should tell you why the query is failing. You could also print the query out, and test it directly in the database, this would generate an error as well.

In case you are lost, this is the query that is failing: according to the code provided anyway, but then again, you didn’t post the whole script as the line failing is line 9, yet it is line 5 in the code provided. However, this is the only query that is being called by the mysqli_fetch_array() function.
[php]$result = mysqli_query($db,“SELECT * FROM members WHERE active = ‘y’ && exempt = ‘n’”) or die(mysqli_error());[/php]

This line should read:
[php]$result = mysqli_query($db,“SELECT * FROM members WHERE active = ‘y’ && exempt = ‘n’”) or die(mysqli_error($db));[/php]

or, better yet, for portability:
[php]$result = mysqli_query($db,“SELECT * FROM members WHERE active = ‘y’ && exempt = ‘n’”) or trigger_error(mysqli_error($db));[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service