Hi Guys I am having trouble on fetching object please see my code below.
Table Animals
-----------------------
id | type | name
-----------------------
1 Cat Muning
2 Kookaburra Bruce
3 Dog Bruce
-----------------------
Animal.php
class Animal extends DatabaseObject{
static $db_fields;
static $table_name = 'animals';
public function animal_group(){
global $database;
$sql = "SELECT animal_name as ani_name, COUNT(animal_name) as quantity FROM " . static::$table_name . " GROUP BY animal_name";
$stmt = $database->query($sql);
return $result = $database->fetch_object($stmt);
}
}
$animal = new Animal();
index.php
<?php
require_once('includes/database.php');
require_once('includes/animal.php');
$ani = $animal->animal_group();
echo $ani->ani_name . " - " . $ani->quantity;
?>
Result
Bruce - 2
What it should be
Bruce - 2
Muning - 1
I also tried While and Foreach but still didn’t work.
1 Answer