Issue
I have a very basic query of listing where I wish to display all the list in a single array
$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo"<pre>";
print_r($row);
echo "</pre>";
}
}
I am getting result as
Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
Array
(
[id] => 2
[email] =>
[password] =>
[status] =>
)
Array
(
[id] => 3
[email] =>
[password] =>
[status] =>
)
However I wish to get the result that looks something like this.
Array
(
[0] => Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
[1] => Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
)
Can anyone please tell how I can obtain this result
Solution
First you need to store all rows into attaylist and then print
$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
$resulyArray = array();
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
array_push($resulyArray , $row);
}
}
echo"<pre>";
print_r($resulyArray);
echo "</pre>";
Answered By – Anant Dabhi
Answer Checked By – Marilyn (BugsFixing Volunteer)