Search code examples
phpsqlfetchall

Save SQL data to PHP array


I want to pull the data from a SQL table to an array in my PHP script. I need that because after that I want to compare two tables.

$sql = "select date, sum(clicks) from Table group by date";

$query = $Db->query($sql);

$result = array(); // Script does not work even if I remove this line

$result = $query->fetchAll();

print_r($result);

I am getting the error :

PHP Fatal error: Call to undefined method mysqli_result::fetchAll()


Solution

  • As @Mark said, use

    $result = $query->fetch_all();
    

    For PHP version prior to PHP 5.3.0, use:

    while ($row = $result->fetch_assoc()) {
        // do what you need.
    }