PHP OOP:函数不将数据库中的所有行返回给PHP

PHP OOP:函数不将数据库中的所有行返回给PHP,php,mysql,Php,Mysql,我创建了一个函数,该函数假设将数据库中的所有行返回到浏览器,但它只返回一个值。以下是我的表格: 这是我的代码: SqlDatabase.php类: public function query($sql){ $result = mysqli_query($this->connection, $sql); return $result; } test.php: $database = new SqlDatabase; //testing func

我创建了一个函数,该函数假设将数据库中的所有行返回到浏览器,但它只返回一个值。以下是我的表格:

这是我的代码:

SqlDatabase.php类:

public function query($sql){

        $result = mysqli_query($this->connection, $sql);

        return $result;
    }
test.php:

$database = new SqlDatabase;
//testing function
    public static function find_comments($photo_id=0){
        global $database;
        $sql  = "select * from comments";
        $sql .= " where photograph_id = {$photo_id}";
        $sql .= " order by created ASC";
        print_r(self::sql_find($sql));
    }
    // This is a testing function
    public static function sql_find($sql){
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_assoc($result_set)){
            $object_array = $row['author'];
        } return $object_array;
    }
当我尝试运行此函数时,它只向PHP返回最后一个值“vai het loc”:

我尝试了几种方法并进行了研究,但还没有找到任何解决方案。不知道我错过了什么。需要一些指南来获取照片中的所有行,而不仅仅是一个值


感谢您的帮助。

您正在覆盖该值,而不是将其附加到数组中

更改此行:

$object_array = $row['author'];
为此:

$object_array[] = $row['author'];

在你的
sql\u find()
函数中,你应该得到这个对象。

Yepp,这很有效。我忘了把[]放在$object_数组中:)谢谢Darren:-)不用担心,很高兴它能帮上忙!:)
$object_array[] = $row['author'];