Inserting Values From Database Into an Array PHP

Inserting Values From Database Into an Array PHP

In this Blog Post, the sample code will perform the below action

(1) Query all the column in the MySQL table

(2) Insert all the queried data results into an Array

(3) Print out all the Data into a table.

Prior to that you need to complete  the items listed Below

  1. Mamp or xampp with phpMyAdmin install into your Computer , check out here
  2.  A Sample Database created Check out Here
  3.  New User Created . Check out Here
  4. A New table created , check out Here
  5. Insert Data into the Table Here

Step 1

Start your My SQL and Apache Server

mysql and Apache Initialised Success
mysql and Apache Initialised Success

Step 2

Copy and paste the below code

 


<style>
table, th, td {
  border: 1px solid black;
}
</style>


<?php

/* Declare User connecting Credential*/

$hostName  ="localhost";
$userName ="jane";
$userPassword ="jane";
$database ="janedb";



/* return connection status through $dbConnectionStatus  Boolean Variable*/

/* using mysqli Object*/

$dbConnectionStatus  = new mysqli($hostName, $userName, $userPassword,$database);


/* Check whether the User Succesfully connect to localhost and mydata Database*/
/* ->  Access the instance "$dbConnectionStatus" variable property "connect_error " boolean */

if ($dbConnectionStatus->connect_error){     

        
     die("Connection failed: " . $dbConnectionStatus->connect_error);

}

// If Connection Status is Success 

if($dbConnectionStatus->connect_error==false){
	
// Table Structure	
	
echo'<table style="width:100%">';
  
  echo'<tr>';
     echo'<th>id</th>';
     echo'<th>First Name</th>';
     echo'<th>Last Name</th>';
     echo'<th>Email</th>';
     echo'<th>Register Date</th>';
   echo'</tr>';
 




//Setup Field Name array . value and key
    {	//	setup ARRAY of field names 
		$personField = array(
					'id' => 'id',
					'firstname' => 'firstname',
					'lastname' => 'lastname',
					'email' => 'email',	
					'reg_date' => 'reg_date',	

					
		);
	}
	
	
	// Select Query
	$sql = " SELECT * FROM mytable";
	// Send Select Query
	$resultData = mysqli_query($dbConnectionStatus,$sql);
	// Declare Array
	$arrayData = array();
	 // Check if results more than 1 row
	 if (mysqli_num_rows($resultData)>0){
		      // while there is still  data in row
			       // assign selected mySQL data into 'arrayData' array 
		      while($row=mysqli_fetch_assoc($resultData)){
				  
		  
				   $arrayData[] = $row; 
				  
				  
			  }
		 
		 
		 
	 }
	 // Foreach datas  assign to $data
	foreach($arrayData as $data){
		
		echo'<tr>';
       // Search through the array print out value if see the Key  eg: 'id', 'firstname ' etc.
        echo'<td>'.$data['id'].'</td>';
        echo'<td>'.$data['firstname'].'</td>';
        echo'<td>'.$data['lastname'].'</td>';
        echo'<td>'.$data['email'].'</td>';
        echo'<td>'.$data['reg_date'].'</td>';
     
     
    echo'</tr>';
		
		
		
		
		
	}

	
        		
echo'</table>';
	 
								
	}

 
 
  

?>



 

Step 3

Save this File as “.php” extension in

C:\xampp\htdocs

Step 4

Test it

(1)Click Admin

(2) Click the  PHP File you have just created  in this example i name it  ” selectData.php”

 

 

Results

Selected Table
Selected Table

 

Leave a Comment

three + ten =