Select data from the MySQL database table using PHP and display in Bootstrap4 table format

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments
  • Post last modified:October 5, 2020
  • Reading time:5 min(s) read

Before we get started, Select data from the MySQL database table using PHP and display in Bootstrap4 table format. So far we have learned how to install the Xammp server and how to create a database and table as well as how to insert data in the previous tutorial.

Now it’s time to select data from the database (MySQL) what data we want to insert. The SQL “SELECT” statement is used to fetch the recorded data from the database table. The simple syntax is given below.

"SELECT * FROM database_tableName";

Now, first of all, open any Text Editor and create a dot (.) PHP file (for example – view.php). Write the Bootstrap 4 code that is given below and can also visit W3school and copy the MaxCDN & paste code after the title tag.

<!DOCTYPE html>
<html>
<head>
<title>fetch data from databse using php</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>	
<div class="container">
<h1>Select data from database using PHP</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>Sl No</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Entry date/time</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Jone Deo</td>
<td>[email protected]</td>
<td>9876543210</td>
<td>22-06-2020</td>
<td>
<a href="" class="btn btn-success">Edit</a>
<a href="" class="btn btn-danger">Delete</a>
</td>
</tr>				
</tbody>
</table>
</div>
</body>
</html>
Select data from the MySQL database table using PHP

The following PHP program is for fetching MySQL data using PHP. But before that, we have to be sure that our database connection is established. If the database connection is not established, the simple code of database connection is given below-

<?php
$con=mysqli_connect('localhost','root','','php');
if ($con) {
    echo "Connection Done";
   }else{
	echo 'Connection Faild';
	}
?>

Once the connection is established then the select code should be written that is stated below. here we used the fetch_assoc() function, it will return as an array of row information containing column values are indexed with the column name. The result type is an associative array.

<?php
	$con=mysqli_connect('localhost','root','','php');
	if ($con) {
		echo "Connection Done";
	}else{
		echo 'Connection Faild';
	}
	/////SELECT QUERY START HERE////////////////
	$sel="SELECT * FROM test";
	$query=$con->query($sel);
	while ($row=$query->fetch_assoc()) 
	{
		///////print data here/////////
	}
?>
Select data from the MySQL database table using PHP
Here is the complete code for select data from the MySQL database table using PHP and display in Bootstrap4 table format.
<!DOCTYPE html>
<html>
<head>
<title>fetch data from databse using php</title>
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>	
<div class="container">
<h1>Select data from database using PHP</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>Sl No</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Entry date/time</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!------connect with databse------>
<?php
$c=1;

$con=mysqli_connect('localhost','root','','php');
if ($con) {
	echo "Connection Done";
}else{
	echo 'Connection Faild';
}

/////SELECT QUERY START HERE////////////////
$sel="SELECT * FROM test";
$query=$con->query($sel);
while ($row=$query->fetch_assoc()) 
{
?>
<tr>
<td><?php echo $c++;?></td>
<td><?php echo $row['fullname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['entry_date'];?></td>
<td>
	<a href="" class="btn btn-success">Edit</a>
	<a href="" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>

Leave a Reply