我创建了一个函数,假设将数据库中的所有行返回给浏览器,但它只返回一个值。下面是我的表格:
这是我的代码:
SqlDatabase.php 类:
public function query($sql){
$result = mysqli_query($this->connection, $sql);
return $result;
}
测试.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;
}
当我尝试运行这个函数时,它只返回最后一个值“vai het loc”给 PHP:
Comments::find_comments(77);
我尝试了几种方法并进行了研究,但尚未找到任何解决方案。不知道我错过了什么。需要一些指南来获取 photo_id 中的所有行,而不仅仅是一个值。
感谢您的帮助。
最佳答案
您正在覆盖该值,而不是将其附加到数组。
改变这一行:
$object_array = $row['author'];
为此:
$object_array[] = $row['author'];
在您的 sql_find()
函数中,您应该获取对象。
关于PHP 面向对象 : Function not return all row in database to php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25800016/