1

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?

2 Answers 2

1

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...
Sign up to request clarification or add additional context in comments.

1 Comment

You were all right i was passing the array to the fetch_array
0

Use this..

$result_set = User::find_by_id(1);

if($result_set){

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

}

3 Comments

Doesn't solve the problem, cf my answer: fetch_array is called twice
i think some times $result_set does not return any value. so that it gives error.. like that..
read the error message carefully: "mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given" <-- array given means OP is passing an array here $database->fetch_array($result_set);, which means $result_set is an array not a result resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.