Retrieve Data from the Database Using PHP/MySQLi

0
161
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“.

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

  1. CREATE TABLE IF NOT EXISTS `tbluser` (
  2. `UserID` int(11) NOT NULL AUTO_INCREMENT,
  3. `Name` varchar(30) NOT NULL,
  4. `Username` varchar(30) NOT NULL,
  5. `Pass` varchar(90) NOT NULL,
  6. PRIMARY KEY (`UserID`)

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

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

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

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

  1. &lt;style&gt;
  2.  
  3. #box{
  4. width: 100%;
  5. }
  6. .column {
  7. width: 33.33%;
  8. display: inline-block;
  9. }
  10. .clear{
  11. clear: left;
  12. height: 15px;
  13. }
  14.  
  15. .table{
  16. width: 100%;
  17. border: solid 1px #ddd;
  18. }
  19. .table tr,
  20. .table td {
  21. position: inherit;
  22. border: solid 1px #ddd;
  23. }
  24.  
  25. &lt;/style&gt;

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

  1. &lt;?php
  2. $server="localhost";
  3. $userid ="root";
  4. $Password = "";
  5. $myDB = "userdb";
  6. $con = mysqli_connect($server,$userid,$Password,$myDB);
  7.  
  8. # code...
  9. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  10. }
  11. ?&gt;

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

  1. &lt;html&gt;
  2. &lt;title&gt;Insert Data Using MySQLi&lt;/title&gt;
  3. &lt;head&gt;
  4. &lt;h1 align="center"&gt;List of Users&lt;/h1&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7.  
  8. &lt;?php
  9. echo '&lt;table class="table" &gt;
  10. &lt;tr&gt;
  11. &lt;td&gt;User ID&lt;/td&gt;
  12. &lt;td&gt;Name&lt;/td&gt;
  13. &lt;td&gt;Username&lt;/td&gt;
  14. &lt;/tr&gt;';
  15. $sqli = "SELECT * FROM `tbluser`";
  16. $result = mysqli_query($con,$sqli) or die('SQL Query error');
  17. while ($row = mysqli_fetch_array($result)) {
  18. echo '&lt;tr&gt;';
  19. echo '&lt;td&gt;'.$row['UserID'].'&lt;/td&gt;';
  20. echo '&lt;td&gt;'.$row['Name'].'&lt;/td&gt;';
  21. echo '&lt;td&gt;'.$row['Username'].'&lt;/td&gt;';
  22. echo '&lt;/tr&gt;';
  23. }
  24. echo '&lt;/table&gt;';
  25.  
  26. &lt;/div&gt;
  27. &lt;/body&gt;
  28. &lt;/html&gt;
  29. &lt;?php mysqli_close($con); ?&gt;

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.

Facebook Comments
(Visited 172 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here