Jump to content

error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bo


Recommended Posts

*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]

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?

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

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.

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");

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.

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

 

 

 

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.

 

 

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");

 

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();

 

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();

  }

}

?>

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.