How to Display Data from MySQL Database into HTML Table using PHP

How to Display Data from MySQL Database into HTML Table using PHP


Hey Programmer today in this tutorial we will
learn how to show data of database on a HTML Table using PHP we are going to make table
like this where data in this table is filled by the data of database by using PHP coding
here is a database which I have created on a previous video I have put the link of that
video on a description to do this start with basic HTML format we are going to make a HTML
table so that if you want to learn about HTML table then click on the i icon on the top
right corner of this video give a title table with database Now create a table and give
heading for each column of table after that for this table data we have to connect it
with database now start with php code for database connection where the server name
is localhost MySQL username is root password is empty and database name is company if error
occur during this connection then we show error message now assign a query on a sql
variable we want to show id username password in a table so that select id username password
from a table name login where id username and password are the heading on a database
table and to execute this query we have to get this result we assign it on result variable
to print a data in a table a database table must contain at least one row or query must
extract at least one row then run a while loop until this condition remain true and
I want to say that we are going to execute one row in each loop so that start with tr
first td for id closing tad second td for username again closing tag at last td for
password and closing tag for td and tr if you have a question why tr td are quotes but
row is not quotes then tr td are not php code but row is php code so that some of them are
quotes and some of them are not then outside of the while loop we close table if this condition
false this means there is zero row then we print zero result at last we close database
connection lets see how its done to make its look better add some css code start with style
copy it and paste it here save it and refresh to make this heading colorful add css code
on th an to make zebra stripe on table use nth child on tr have a question there is a
comment section press like if you enjoy goodbye programmer

Danny Hutson

100 thoughts on “How to Display Data from MySQL Database into HTML Table using PHP

  1. Hi bro,
    Can u please make a video on how to display the data as a drop down from database using php in wordpress

  2. <!DOCTYPE html>
    <html>
    <head>
    <title>Table with database</title>
    <style>
    table {
    border-collapse: collapse;
    width: 100%;
    color: #588c7e;
    font-family: monospace;
    font-size: 25px;
    text-align: left;
    }
    th {
    background-color: #588c7e;
    color: white;
    }
    tr:nth-child(even) {background-color: #f2f2f2}
    </style>
    </head>
    <body>
    <table>
    <tr>
    <th>Id</th>
    <th>Username</th>
    <th>Password</th>
    </tr>
    <?php
    $conn = mysqli_connect("localhost", "id7158040_root", "", "id7158040_demo2");
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    $sql = "SELECT id, username, userpassword FROM teacher";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>"
    . $row["userpassword"]. "</td></tr>";
    }
    echo "</table>";
    } else { echo "0 results"; }
    $conn->close();
    ?>
    </table>
    </body>
    </html>

    this is my code but my table looks like this: https://imgur.com/a/OT546JF

  3. Why do you need to add dot before and after the variable because i thought you only do this if you are joining to something else..

    I also guess that because you open the table outside the while loop.. you need to closed it after the while loop?

  4. I can't figure out how to add 3 separate tables here… Instead of having one table, how would I do this? Can I share with you my code here?

  5. I managed to get it to work but with regard to doing a responsive table, I added overflow-x:auto; but this only works for some tables and not all. Should it work for all the tables?

  6. changed your example to my database, but get the error message below:

    Notice: Trying to get property of non-object in D:DataOneDriveOneDriveBureaubladWebserverfolderroottest.php on line 49

    0 result
    line 49 : if ($result-> num_rows > 0) {

    below my test.php file

    <!DOCTYPE HTML>

    <HTML>

    <HEAD>

    <title> GP_OB tabel</title>

    <style>

    table{

    border-collapse: collapse;

    width: 100%

    color: #588c7e;

    font-family: mnospace;

    font-size: 25px;

    text-align: left;

    }

    th {

    background-color: #088A08;

    color: white;

    }

    tr:nth-child(even) {background-color: #f2f2f2}

    </style>

    </head>

    <body>

    <table>

    <tr>

    <th>Famnm</th>

    <th>Voornm</th>

    <th>Geboren_Te</th>

    <th>Datum_Geb</th>

    <th>Overleden_Te</th>

    <th>Datum_Ov</th>

    <th>Foto_GP</th>

    <th>Foto_OB</th>

    <th>ID</th

    </tr>

    <?php

    $conn = mysqli_connect("localhost", "root", "usbw", "test");

    if ($conn-> connect_error){

    die("Connection failed:". $conn-> connect_error);

    }

    $sql = "select ID, Famnm , Voornm , Geboren_Te, Datum_Geb, Overleden_Te, Datum_Ov, Foto_GP, Foto_OB from login";

    $result = $conn-> query($sql);

    if ($result-> num_rows > 0) {

    while ($row = $result-> fetch_assoc()) {

    echo "<tr><td>".$row["id"]

    ."<td><td>".$row["Famnm"]

    ."<td><td>".$row["Voornm"]

    ."<td><td>".$row["Geboren_Te"]

    ."<td><td>".$row["Datum_Geb"]

    ."<td><td>".$row["Overleden_Te"]

    ."<td><td>".$row["Datum_Ov"]

    ."<td><td>".$row["Foto_GP"]

    ."<td><td>".$row["Foto_OB"]

    ."</td></tr>";

    }

    echo "</table>";

    }

    else {

    echo "0 result";

    }

    $conn-> close();

    ?>

    </table>

    </body>

    </html>

  7. i have a row for messages, so, if i write a long message, it creates one long line, and i have to scroll to read it. how can i limit the words to break at some point? i tried "word-wrap:break-word;" and "table-layout: fixed;" and it works but it creates long white spaces in each row

  8. Thanks for that can you explain us how can display a singel submit data one by one look like a html resume and how can search a single submit data in html

  9. I am so thankful to you for giving the source code. Many people have uploaded the videos but there is no source code like this. I am a slow learner and I need this kind of source file so I can easily follow and do this by myself. Thank you sir. I am subscribing to your channel right now. Blessings.

  10. Sir do you have source code for how to display a single record and then edit it. There are many videos but no source code. It is easy for old people like me to follow with source code and i can work in my slow pace. Thank you and bless you.

  11. Trying to get property 'num_rows' of non-object in C:xampphtdocsZadatak2spajanje_na_bazu.php on line 30

    0 results

    ITS NOT WORKING STOP SCAMMING PEOPLE!

  12. hi, i need particular row to display like a form page ….please tell what are function are needed for that

  13. thank you for this good tutorial , i have a problem i m using a template from web the table d'ont show me result but print my code as string … i try to make my code external in new php file the table its working and print my data from data base !! can you help me please

  14. Thanks. I really needed this part for my project. You sound like a Nepali from your accent. Are you a Nepali?

  15. hey great video for whatever reason the actual values from my table arent displaying. the html table headers show and ive tested my php installation and that is working too. im wondering if somehow mysql isn't connecting properly but its not displaying a connection error at all any ideas? thanks

  16. Hi, that's a good video. I am not a good in codes. But I have a requirement to do a html link with connection in database and to fetch the data in html link. How i can i approach you for my requirements.

  17. Love you thank you very much very very very very very very very very very veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy nice video

  18. bro awesome very useful tutorials.

    make a form for Blood donar registration. and fetching the details blood group wise

  19. i followed the ur pervious code and succesly connected the databse with a images also. kindly help to display the same on webpage

  20. ( ! ) Notice: Undefined index: S.No in C:wampwwwphpprojectnewmath.php on line 45

    I'm getting this error…please help to solve this error

  21. Im getting 0 result. Below is my code. connection details left out on purpose.

    $conn = new PDO("mysql:host=localhost;dbname=", "", "");
    $sql = "SELECT tittle, link, tag from video";
    $conn-> query($sql);

    if($result->num_rows > 0){
    while($row = $result-> fetch_assoc()){
    echo "<tr><td>". $row["tittle"]."</td><td>". $row["link"]."</td><td>".$row["tag"]."</td></tr>";
    }
    echo "</table>";
    }
    else{
    echo "0 result";
    }

  22. Hi, great video! I have followed every step and it has worked ALMOST perfectly; the column names or headings as you call them aren't appearing. I can't figure out why. Have you any idea why it could be?

  23. Hi, I need some help.
    Can I do this using a div structure?
    If the answer is yes, do you have any video in your channel about it or a link?
    Thanks

  24. I want to get some input (let contact no.) from user and based on that value display only associated row from database

Leave a Reply

Your email address will not be published. Required fields are marked *