ferlicia Posted July 20, 2011 Share Posted July 20, 2011 *Hi, I got this error. Please help. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in $result_1= mysqli_query($link, "SELECT $criteria FROM table ORDER BY '".$criteria."' ASC); $array1 = array(); while($row = mysqli_fetch_array($result_1)){ array_push($array1, "$row[0]->$criteria", "$row[5]->$criteria" , "$row[10]->$criteria"); } i wanted to arrange the criteria that are selected by the user from a dropdown box, therefore i wanted to arrange it in ACS order, and then put inthe data into the row. but i don't seem to get it right and there is a lot of error. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/ Share on other sites More sharing options...
dcro2 Posted July 20, 2011 Share Posted July 20, 2011 You're missing an ending double quote in your query. But what exactly would $criteria contain? I can't figure out your logic in the array_push() call. $row would contain one row, with whatever number of criteria you selected. I'm not sure why you're accessing indexes 0, 5, and 10 and then accessing a submember? Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245175 Share on other sites More sharing options...
ferlicia Posted July 20, 2011 Author Share Posted July 20, 2011 i have added in the missing double quote, but the error is still there and it is still the same error. actually for my assignment, i have to arrange the criteria that the user select in average, there will be 5 group, therefore i am thinking of getting the best 1 with the worst from the selection. please help Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245186 Share on other sites More sharing options...
AyKay47 Posted July 20, 2011 Share Posted July 20, 2011 you are using a variable in your mysqli query with improper syntax.. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245192 Share on other sites More sharing options...
dcro2 Posted July 20, 2011 Share Posted July 20, 2011 Yeah, I don't really think you understand what you're doing with that query. It's failing, so $result_1 is false. First of all, when you specify field names in a query, you either put nothing around them or wrap them in backticks (`). $result_1 = mysqli_query($link, "SELECT $criteria FROM table ORDER BY $criteria ASC"); Second, let's say $criteria is equal to "name". What you'll get back from that query is the `name` field of all the rows in the table. So, when you fetch a row, then $row will be an array, containing: [0] => "value of `name` for current row" And that's it. Now, if $criteria contains more than one field, then $row will contain one consecutive index (0, 1, 2, etc) for each field selected. If you explain what it is you want to fetch, we might be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245217 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 i wanted to fetch the data that i have selected using the form, and put it in the array. like for school, when the teacher select the criteria that he wanted to group the student, then after selecting it, i will use the query to arrange the selected criteria in ASC order. after arranging i will out it into rows $result_1 = mysqli_query($link,"SELECT $criteria FROM criteria ORDER BY '".$criteria."' ASC"); $team1=Array(); $team2=Array(); while($row = mysqli_fetch_array($result_1)){ array_push($team1, "$row[0]->$criteria", "$row[5]->$criteria", "$row[10]->$criteria", "$row[19]->$criteria", "$row[24]->$criteria"); array_push($team2, "$row[1]->$criteria", "$row[6]->$criteria", "$row[11]->$criteria", "$row[18]->$criteria", "$row[23]->$criteria"); Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245437 Share on other sites More sharing options...
dcro2 Posted July 21, 2011 Share Posted July 21, 2011 A more complete example would be nice, like what you expect the output to look like. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245441 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 i will expect that the output will be that the student will be arranged in groups that are in the particular row Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245442 Share on other sites More sharing options...
dcro2 Posted July 21, 2011 Share Posted July 21, 2011 Okay, let me be more specific. Post what a few rows from the table would look like, something like this: column1 column2 column3 thesearevalues thesearevalues thesearevalues and then what you would want your output to LOOK like, based on those rows. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245444 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 team 1 student1 student6 student11 student20 student25 team 2 student2 student7 student12 student19 student24 actually there is only 1 column with 25 rows like for example class student1 student2 student3 student4 student5 student6 student7 student8 student9 student10 student11 student12 student13 student14 student15 student16 student17 student18 student19 student20 student21 student22 student23 student24 student25 Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245445 Share on other sites More sharing options...
darkfreaks Posted July 21, 2011 Share Posted July 21, 2011 fixed msising an ending double quote " $result_1= mysqli_query($link, "SELECT $criteria FROM table ORDER BY '".$criteria."' ASC") or die("MySQL error: " . mysqli_error($link) . "<hr>\nQuery: $result_1"); please let us know if you get a MYSQLI error. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245474 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 there is still the same error, on these line while($row = mysqli_fetch_array($result_1)){ (error) array_push($team1, "$row[0]->$criteria", "$row[5]->$criteria", "$row[10]->$criteria", "$row[19]->$criteria", "$row[24]->$criteria"); array_push($team2, "$row[1]->$criteria", "$row[6]->$criteria", "$row[11]->$criteria", "$row[18]->$criteria", "$row[23]->$criteria"); Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245475 Share on other sites More sharing options...
darkfreaks Posted July 21, 2011 Share Posted July 21, 2011 try revamping your connection using OOP define('HOST','localhost'); define('USER','root'); define('PASS',''); define('DB','fyp'); class foo_mysqli extends mysqli { public function __construct($host, $user, $pass, $db) { parent::__construct($host, $user, $pass, $db); if (mysqli_connect_error()) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } } } $db = new foo_mysqli(HOST, USER, PASS,DB); $db->close(); Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245479 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 for not there is an error on this line $mysqli->close(); this error: Fatal error: Call to a member function close() on a non-object in Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245480 Share on other sites More sharing options...
darkfreaks Posted July 21, 2011 Share Posted July 21, 2011 found a extender class OOP that you can use easily and modify into your code. mysqli extender class <?php /* Class: DbConnector Purpose: Connect to a database, MySQLi version */ require_once 'SystemComponent.php'; class DbConnector extends SystemComponent { var $myMySQLi = null; /* Function: __construct Purpose: Connect to the database */ function __construct() { // Load settings from parent class $settings = parent::getSettings(); // Get the main settings from array we just loaded $host = $settings['host']; $user = $settings['user']; $pass = $settings['pass']; $db = $settings['name']; // Connect to the database $this->$myMySQLi = mysqli_init(); $this->$myMySQLi->real_connect($host, $user, $pass, $db); } /* Function: query Purpose: Execute a database query */ function query($query) { return $this->$myMySQLi->query($query); } /* Function: fetchArray Purpose: Get array of query results */ function fetchArray($result) { return $this->$myMySQLi->result->fetch_array(); } /* Function: close Purpose: Close the connection */ function close() { $this->$myMySQLi->close(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245481 Share on other sites More sharing options...
ferlicia Posted July 21, 2011 Author Share Posted July 21, 2011 sorry i dun understand about the statement changed the above OOP to include a parent class how should i do it ? Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245482 Share on other sites More sharing options...
darkfreaks Posted July 21, 2011 Share Posted July 21, 2011 also is your table named table? this is a MYSQL reserved word and it will throw errors if you do not rename it. Quote Link to comment https://forums.phpfreaks.com/topic/242438-error-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-bo/#findComment-1245484 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.