嗨,伙计们,我在获取对象时遇到问题,请参阅下面的代码。
餐桌动物
-----------------------
id | type | name
-----------------------
1 Cat Muning
2 Kookaburra Bruce
3 Dog Bruce
-----------------------
动物.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();
索引.php
<?php
require_once('includes/database.php');
require_once('includes/animal.php');
$ani = $animal->animal_group();
echo $ani->ani_name . " - " . $ani->quantity;
?>
结果
Bruce - 2
它应该是什么
Bruce - 2
Muning - 1
我也尝试过 While 和 Foreach 但仍然没有用。