How to display data from database in PHP

How to display data from database in PHP

Steps to display data from database in PHP

How to display data from database in PHP
How to display data from database in PHP
How to display data from database in PHP
How to display data from database in PHP
How to display data from database in PHP
How to display data from database in PHP
<?php // Establish the connection with Mysql databae $servername='localhost'; $username="root"; $password=""; 
try
{
$con=new PDO("mysql:host=$servername;dbname=student_db",$username,$password); $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//echo 'connected';
}
catch(PDOException $e)
{
echo '<br>'.$e->getMessage();
} ?>
<?php //PHP script to display the table data 
$stmt = $con->prepare("select * from student_info");
$stmt->execute();
$student_details = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
How to display data from database in PHP
<?php // Establish the connection with Mysql databae $servername='localhost'; $username="root"; $password=""; try { $con=new PDO("mysql:host=$servername;dbname=student_db",$username,$password); $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
//echo 'connected';
} catch(PDOException $e)
{
echo '<br>'.$e->getMessage();
}
//PHP script to display the table data
$stmt = $con->prepare("select * from student_info");
$stmt->execute();
$student_details = $stmt->fetchAll(PDO::FETCH_ASSOC); ?
>
<!DOCTYPE html>
<html lang="en">
<head>
title>Bootstrap Example</title>
<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container" style="width:50%;">
<h2>Student Information</h2>
<p>Below table shows the student data:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Sl</th>
<th>Name</th>
<th>Age</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody>
<?php
if($student_details)
{
foreach($student_details as $key=>$value)
{
?>
<tr>
<td><?php echo $key+1 ;?></td>
<td><?php echo $value['name'] ;?></td>
<td><?php echo $value['age'] ;?></td>
<td><?php echo $value['phone_no'] ;?></td>
</tr>
<?php
}
}
else
{
?>
<tr>Data not found</tr>
<?php
}
?>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </body> </html>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.