WeBBy421 Posted April 1, 2016 Share Posted April 1, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/ Share on other sites More sharing options...
taquitosensei Posted April 1, 2016 Share Posted April 1, 2016 (edited) What data type is your active column and is it storing 'y' and 'n' or is it 1 and 0? Plus I've never used && before. Not sure that works. I've always used "and". Edited April 1, 2016 by taquitosensei Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532709 Share on other sites More sharing options...
benanamen Posted April 1, 2016 Share Posted April 1, 2016 (edited) 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 April 1, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532710 Share on other sites More sharing options...
mac_gyver Posted April 1, 2016 Share Posted April 1, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532711 Share on other sites More sharing options...
WeBBy421 Posted April 2, 2016 Author Share Posted April 2, 2016 Thanx that answers a few questions, but maybe it is my N00bie status, but these answers realy do not tell me how to fix the error Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532722 Share on other sites More sharing options...
WeBBy421 Posted April 2, 2016 Author Share Posted April 2, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532724 Share on other sites More sharing options...
ginerjm Posted April 2, 2016 Share Posted April 2, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301136-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result/#findComment-1532742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.