Jump to content

mysqli_fetch_array() expects parameter 1 to be mysqli_result


WeBBy421

Recommended Posts

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 8

 

Here is the script:

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<br>";
11 unset($id);
12 }
13 }

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

Link to comment
Share on other sites

Using extract like that is bad practice. All of a sudden your script has variables that just magically appear out of thin air. It will make debugging much harder to do when you don't know where stuff is coming from especially when the code base grows. Your active column should be 1 or 0, not Y and N and column type tinyint. You should also explicitly ask for your columns and not use *.

Edited by benanamen
Link to comment
Share on other sites

the error is because you are reusing the loop control variable, $result, inside the loop. specifically in the line that when you comment it out, the loop runs.

 

the update query returns a Boolean true when the update query runs without any errors. since you are not using the result from the update query that you are assigning to that variable, why are you even assigning it to a variable?

 

also, mysqli_error(....) requires the database connection link as a parameters. if either of your queries would fail for some reason, you would just be getting another error due to the incorrect usage of the mysqli_error() statement. you should actually be using exceptions to handle query errors and avoid using all the or die() logic in your code. your main code will only deal with error free execution of the database statements.

Link to comment
Share on other sites

Just as a foillow-up...

Am using the exact same parameters after the above query to send emails to all those in databse with exempt="n" without any errors (for reasons I cannot go into cannot combine the scripts). So, I am still unsure why thsi one goes into error but the one that follows does not.

The structure of the column being changed is enum('y', 'n').

Adding $db to the mysqli_error() does not increase error info.

Also, no error if no db changes made

Thanx again

Link to comment
Share on other sites

As a newbie you should make the effort to learn by picking up the manual or a good book and giving it a reading. Look at the syntax sections, how to construct a database 'process' as in: the connection, the query statement preparation, the query, checking the results of the query and then fetching the rows from the query results. All good stuff to learn by yourself and THEN trying to make it work for you and THEN asking for help with it. If you can't understand what you are told, how can we help you?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.