Bairdy Bairdy - 9 days ago 5
MySQL Question

Return from foreach loop

I'm new to PHP. I have a dummy database with three users and I want to insert their keys and values into an array.

$result_set=$database->GeneralQuery("SELECT * FROM users");
$the_object_array=array();
while ($row=mysqli_fetch_assoc($result_set)) {
foreach($row as $key => $value){
$the_object_array[$key] = $value;
}
}
print_r($the_object_array);


Which returns:-

Array ( [id] => 3 [username] => anne [password] => 123 [first_name] => Anne [last_name] => Baird ).....


In other words only the last user in the database table with keys and values.

How can I get
print_r($the_object_array)
to print out all three users with keys and values

Answer

Try following code

$result_set=$database->GeneralQuery("SELECT * FROM users");
$the_object_array=array();
while ($row = mysqli_fetch_assoc($result_set)) { 
   $the_object_array[] = $row; 
} 
print_r($the_object_array);
Comments