Please Login or Register to continue.


I have a situation where I want to fetch table row data and store it in an array using the foreach() loop in PHP. In simple words, I want to fetch MYSQL table data and store fetched data in an array using a foreach() loop. Is there any other method to store all elements one by one or put all elements one by one into an array in PHP? I am writing code in PHP programming and using the MYSQL database.

How can I fetch data from the database and store it in an array using foreach() loop? 

(2.4K Points)
in PHP


2 Answers
(9.4K Points)

It's really easy to process if you want to save your data into an array using a foreach loop. First of all, fetch all data from the database. I don't know if you are using PHP MySQLi or PHP PDO but this method will work with both. As you know, we can use a while loop or foreach loop to get the array elements value. Fetch database table data into an array and use foreach loop and put data into a new array like this - 

$newArray=array(); // declare array before foreach loop  
foreach($rows as $row) 
//your database data 

//Put variable value in an array . 
//Use array anywhere outside the foreach loop

echo var_dump($newArray); 

In the code above, you must declare a new array outside the foreach loop (before the foreach loop) and put an element inside the foreach loop after that you can use a new array anywhere in your program. 


(3.4K Points)

The foreach loop is used for arrays. If you want to fetch data from the MYSQL database and want to store a column or multiple columns (put) in the new array then you should declare an array outside the foreach loop . 

Fetching data from the database and saving in an array using foreach loop - 

First of all, create an MYSQL database table using the below query - 

CREATE TABLE `products` (
  `title` varchar(255) DEFAULT NULL,
  `price` double NOT NULL

Insert some data using the below query - 

INSERT INTO `products` (`id`, `title`, `price`) VALUES
(1, 'Mobile Phone Xyz', 12000),
(2, 'Laptop 5i', 90000),
(3, 'Tablet 8 GB ', 22999);

If you want to fetch data from the database then create a connection file. We will use a PHP PDO connection

Create a file config.php . 


<?php session_start();
try {
  $conn = new PDO("mysql:host=".$databaseHost.";dbname=".$databaseName, $databaseUser, $databasePassword);
  //echo 'PDO Connected Successfully'; 
} catch(PDOException $error) {
  //echo "Something went wrong " . $error->getMessage();

Now, we will fetch the table data using the query and will save table column data in a new array using the foreach loop in PHP. 

Create a file index.php


 $stmt = $conn->query('SELECT * FROM products ORDER BY id DESC');
        foreach($rows as $row) {         
         echo $row['title'].'<br>';
         //Save data in an array using foreach loop 
         echo var_dump($prices); 
         // $prices is a new array 

As you can see in the above code, we declared a new array before the foreach loop and used it inside the foreach loop to save price values one by one using the loop. 

You can use this new array after the foreach loop. 

In this way, you can fetch data from the database and put it in a new array using a foreach loop. 


Related Questions :-

Featured Items:-

Certificate system in PHP website | PHP scripts


Home Credito loan finance management multipurpose system in PHP


Result management system with Marksheet in PHP website | PHP Scripts


Your Answer

You can post your answer after login..