jcromanu
jcromanu

Reputation: 1461

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given

in this case my queries work fine i have tested them on mysql.

First I have a custom query function that looks like this in the MySQLDatabase class

public function query($query) {
    $this->last_query = $query;
    $resultado = mysqli_query($this->connection, $query);
    $this->confirm_query($resultado);
    return $resultado;
}

private function confirm_query($result) {
    if ($result  === FALSE) {
        print_r(mysqli_fetch_assoc($result));
        $output = "Error on query".  mysqli_error($this->connection)."<br/>";
        $output .= "Last query : ".$this->last_query;
        die($output);
    }
}

public function fetch_array($result_set){

    return mysqli_fetch_array($result_set);

}

Then i have a class called User which use the MYSQL class

    public static function find_by_id($id=0){
    global $database;
    $result_set = $database -> query("SELECT * FROM users WHERE id={$id}");
    $found = $database ->fetch_array($result_set);
    return $found;
}

And finally i call the methods in my index.php in this way

$result_set = User::find_by_id(1);
$found_user = $database ->fetch_array($result_set);

Here are some facts : 1.- This warning happens when I insert an id value that exists in my database

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given

2.- This warning happens when I insert an id value that doesn´t exists in my database.

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given

The conclusion is that mysqli_query is not returning results but an result object like this in the case it finds a row in the database with certain id .

mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 1 [type] => 0 )

How do I get rid of this error?

Upvotes: 1

Views: 1121

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

The found_by_id method is already calling mysqli::fetch_array here:

$found = $database ->fetch_array($result_set);

It returns the fetched array, if you then write:

$result_set = User::find_by_id(1);
$found_user = $database ->fetch_array($result_set);//fetch_array again

you're passing the fetched array back to fetch_array, which doesn't work

It's exactly the same as writing this:

$result_set = $database -> query("SELECT * FROM users WHERE id=1");
$found = $database ->fetch_array($result_set);
$found_user = $database->fetch_array($found);//<-- second call...

Upvotes: 1

SuReSh
SuReSh

Reputation: 1511

Use this..

$result_set = User::find_by_id(1);

if($result_set){

      $found_user = $database ->fetch_array($result_set);

}

Upvotes: 0

Related Questions