Store Data in an Array using PHP with example

In this article, you going to see how to store data in array with example using PHP. Example is explained in simple way.

Let’s start with adding of value in array step by step: (Store Data Array PHP)

For example point of view to adding data to array going to create a table in a database.

After creating a table in database going to fetch the data from database.

Now next step is to do with the help of while loop going to add all the value into array using PHP.

So let’s start with important point how to add data.

To do this we first going to create table named employ.

CREATE TABLE `employ` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

After executing above SQL query table is created in database.

Created Table
Created Table

Now it important point to insert value in the database.

To insert data into database execute the INSERT SQL STATEMENT.

For more details regarding insert statement you can check with this link. INSERT SQL

Below is syntax for insert SQL statement.

INSERT INTO table_name
VALUES (value1, value2, ...);

Below is example for insert sql query.

INSERT INTO `employ` (`id`, `name`, `email`, `phone`, `created_at`) VALUES
(1, 'Rocky', '[email protected]', '9898989988', '2020-06-21 04:44:13'),
(2, 'somo', '[email protected]', '9898989912', '2020-06-13 18:30:00'),
(3, 'Ronit', '[email protected]', '9899898989', '2020-06-22 10:31:13'),
(4, 'Rahul', '[email protected]', '9898111122', '2020-06-22 10:50:34');

With help of above query data get inserted.

Data Get Inserted
Data Get Inserted

Below is complete code snippet from which you come to understand for adding of value in array.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Store value in array</title>
</head>
<body>
<?php
   /* database configuaration */
   define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', '');
   define('DB_DATABASE', 'phpdatabase');
   $con  = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

   // Check connection
	if (mysqli_connect_errno()) {
	  echo "Failed to connect to MySQL: " . mysqli_connect_errno();
	  exit();
	}else{
		$arrData = array();
		$sql = "SELECT * FROM employ";
       if($result = mysqli_query($con, $sql)){
			if(mysqli_num_rows($result) > 0){
				while($row = mysqli_fetch_array($result)){
					$arrData[] = $row['name']; 
					$arrData[] = $row['email']; 
					$arrData[] = $row['phone']; 
				}
				print_r($arrData);
			}
	   }
	}
?>
</body>
</html>

In the above code snippet first you have to add the database details.

It is for the connection between database and front-end.

After the connection is made successfully.

Then create variable array in which we have to store value.

After creating variable execute select SQL statement to fetch data from database.

Then with the help of while loop we going to add this data to the array variable.

In the while loop we adding value such as name, email and the phone number.

Before executing while loop check with number of record are present in table with the help of mysql_num_rows() function.

Below is the output of above example.

Array ( [0] => Rocky [1] => [email protected] [2] => 9898989988 [3] => somo [4] => [email protected] [5] => 9898989912 [6] => Ronit [7] => [email protected] [8] => 9899898989 [9] => Rahul [10] => [email protected] [11] => 9898111122 )

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment