Reputation: 1597
I've created a function that suppose to return all row in database to browser, but it is return only one value. Below is my table:
And this is my code:
SqlDatabase.php Classes:
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;
}
When i try to run this function, It is only return one last value "vai het loc" to PHP:
Comments::find_comments(77);
I tried several ways and do researchs but haven't found any solutions yet. Not sure what am i missing. Need some guide to fetch all row in photograph_id not only one value.
Thanks for helps.
Upvotes: 2
Views: 542
Reputation: 13128
You're overwriting the value, not appending it to the array.
Change this line:
$object_array = $row['author'];
to this:
$object_array[] = $row['author'];
In your sql_find()
function and you should get the object.
Upvotes: 8