In this PHP example we will learn how to write a php file to fetch data from mysql database table with PHP query.
This example we will create two php script files one is for Create Database connection and queries and other one is for parse the MySql query response data and display data on the UI.
Databse Connection
<?php
session_start();
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'ebridge');
class DB_con
{
function __construct()
{
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
$this->dbh=$con;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
public function get()
{
$ret=mysqli_query($this->dbh,"select * from userdata");
return $ret;
}
}
?>
|
Parse MySql response on display on UI
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>RRTutors | DEMO</title>
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="https://rrtutors.com/uploads/php/css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="https://rrtutors.com/uploads/php/css/styles.css" rel="stylesheet">
<style>
table {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2;}
tr:hover {background-color: #ddd;}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04AA6D;
color: white;
}
</style>
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" rel="home" href="https://rrtutors.com/">RRTutors</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
</nav>
<div class="container-fluid">
<!--/left-->
<!--center-->
<div class="col-sm-6">
<div class="row">
<div class="col-xs-12">
<h3>Fetch Data from Database using PHP MySql</h3>
<?php
include_once("data-xyz.php");
$insertdata=new DB_con();
$sql=$insertdata->get();
?>
<table>
<?php
while ($data = $sql->fetch_assoc())
{
?> <tr>
<td><?php echo $data['id'];?> </td>
<td><?php echo $data['name'];?> </td>
<td><?php echo $data['email'];?> </td>
<td><?php echo $data['addrss'];?> </td>
</tr><?php
}
?>
</table>
</div>
</div>
<hr>
</div>
<div class="col-sm-6">
</div>
<!--/center-->
</div><!--/container-fluid-->
<!-- script references -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://rrtutors.com/uploads/php/js/bootstrap.min.js"></script>
</body>
</html>
|
You can find the complete source code below
Article Contributed By :
|
|
|
|
891 Views |