Retrieve Data from the Database Using PHP/MySQLi

Retrieve Data from the Database Using PHP
Recover 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“.

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

[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]

&lt;style&gt;

#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;
}

&lt;/style&gt;

[/css]

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

[php]

&lt;?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();
}
?&gt;

[/php]

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

[php]

&lt;html&gt;
&lt;title&gt;Insert Data Using MySQLi&lt;/title&gt;
&lt;head&gt;
&lt;h1 align="center"&gt;List of Users&lt;/h1&gt;
&lt;/head&gt;
&lt;body&gt;

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

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php mysqli_close($con); ?&gt;
[/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