Problem with return of fetch_all method of PHP MySQLi class

1

So, I'm doing maintenance on a very large system. I have the query:

$query = $mysqli->query("SELECT*FROM tabela");

And the problem is that they used fetch_all() to return the data. When the query does not return records the fetch_all() method is not defined in the mysqli_result class. When the query does not return records and I try to call it, I have the undefined method warning.

I've already done something like:

$resultado = $query->num_rows > 0 ? $query->fetch_all() : array();

This causes $resultado to turn a blank array if the query does not have records returned. the problem is that there are many lines of code that should be edited. Is there a way to "create" this fetch_all() method on the object and make it return a blank array if the query does not return records? (same or any other alternative).

@Edit

I found that mysqlnd is missing. But the question follows: how can fetch_all() be called even if it does not exist?

Thankful

    
asked by anonymous 02.01.2017 / 17:29

1 answer

1

I was able to do the following:

<?php

class MySQLiConnector extends \mysqli
{
    function __construct($host, $user, $pass, $db)
    {
        parent::__construct($host, $user, $pass, $db);
    }

    public function query($query) {
        if( !$this->real_query($query) ) {
            throw new exception( $this->error, $this->errno );
        }

        return new MySQLiResultSet($this);
    }
}

class MySQLiResultSet extends \MySQLi_Result
{
    public function fetch()
    {
        return $this->fetch_assoc();
    }

    public function fetch_all()
    {
        $rows = array();
        while($row = $this->fetch())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}

and I instantiated my MySQLiConnector class instead of mysqli on my connection object.

    
02.01.2017 / 19:13