Retrieve Data from the Database Using PHP/MySQLi

Retrieve Data from the Database Using PHP
Retrieve Data from the Database Using PHP

In this tutorial, i will teach you how to Retrieve Data from the Database Using PHP/MySQLi. This method will help you fetch data from MySQL database to HTML table</tag> view by using SQL SELECT Statement in PHP and MySQLi.

Follow the step by step guide to know on how to Retrieve Data from the Database Using PHP/MySQLi.

Lets Begin:

First Step: Create a Database and name it “userdb“.

Online Examination System in PHP wi...
Online Examination System in PHP with Source Code 2021 | PHP Projects Free Download

Second Step: Create a table in the database that you have create.

Retrieve Data From MySQL Using PHP

[mysql]

CREATE TABLE IF NOT EXISTS `tbluser` (
`UserID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(30) NOT NULL,
`Username` varchar(30) NOT NULL,
`Pass` varchar(90) NOT NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

[/mysql]

Third Step: Do the following code for inserting date in the table of the database.

[mysql]

INSERT INTO `tbluser` (`UserID`, `Name`, `Username`, `Pass`) VALUES
(1, ‘Janno Palacios’, ‘janobe’, ‘admin’),
(2, ‘Joken villanueva’, ‘joken’, ‘joken’),
(3, ‘kejie palacios’, ‘kenjie’, ‘kenjie’);

[/mysql]

Fourth Step: Create a landing page and name it “index.php“.

Fifth Step: Do the following codes for the CSS Style of the page.

[css]

<style>

#box{
width: 100%;
}
.column {
width: 33.33%;
display: inline-block;
}
.clear{
clear: left;
height: 15px;
}

.table{
width: 100%;
border: solid 1px #ddd;
}
.table tr,
.table td {
position: inherit;
border: solid 1px #ddd;
}

</style>

[/css]

Sixth Step: Set a connection of MySQL Database to PHP using MySQLi Extension.

[php]

<?php
$server=”localhost”;
$userid =”root”;
$Password = “”;
$myDB = “userdb”;
$con = mysqli_connect($server,$userid,$Password,$myDB);

if (mysqli_connect_errno()) {
# code…
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
?>

[/php]

Finally, do the following codes for retrieving data from the database.

[php]

<html>
<title>Insert Data Using MySQLi</title>
<head>
<h1 align=”center”>List of Users</h1>
</head>
<body>

<?php
echo ‘<table class=”table” >
<tr>
<td>User ID</td>
<td>Name</td>
<td>Username</td>
</tr>’;
$sqli = “SELECT * FROM `tbluser`”;
$result = mysqli_query($con,$sqli) or die(‘SQL Query error’);
while ($row = mysqli_fetch_array($result)) {
echo ‘<tr>’;
echo ‘<td>’.$row[‘UserID’].'</td>’;
echo ‘<td>’.$row[‘Name’].'</td>’;
echo ‘<td>’.$row[‘Username’].'</td>’;
echo ‘</tr>’;
}
echo ‘</table>’;

</div>
</body>
</html>
<?php mysqli_close($con); ?>
[/php]

If you have any questions or suggestion about on how to Retrieve Data from the Database Using PHP/MySQLi, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

Leave a Comment