Display Data From Database in PHP

To learn how to display data from database using PHP you must know how to insert data into database using PHP so that before reading this post you have to read my previous post to insert data into database using PHP. If you already read the post how to insert data into database then directly come here to learn how to display data from database in table, to do this you can use the below PHP code. It will display all the data from your database table.

Step1: Index.php
<!DOCTYPE html>

    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Home Page</title>
    </head>

    <body>
        <form action="display.php" method="post">
            <input type="submit" value="Search">
        </form>
     </body>
    </html>

Step2: display.php
    <?php

    $link = mysqli_connect("localhost", "root", "root", "employee_db");

    if($link === false){

        die("Error: " . mysqli_connect_error());

    }

     $sql = "select * from employee";

    if($result = mysqli_query($link, $sql)){

        if(mysqli_num_rows($result) > 0){

            echo "<table>";

                echo "<tr>";

                   echo "<th>first_name</th>";

                    echo "<th>last_name</th>";

                    echo "<th>email_address</th>";

                echo "</tr>";

            while($row = mysqli_fetch_array($result)){

                echo "<tr>";

                    echo "<td>" . $row['first_name'] . "</td>";

                    echo "<td>" . $row['last_name'] . "</td>";

                    echo "<td>" . $row['email_address'] . "</td>";

                echo "</tr>";

            }

            echo "</table>";

          mysqli_free_result($result);

        } else{

            echo "No records matching your query were found.";

        }

    } else{

        echo "Error: Not able to execute $sql. " . mysqli_error($link);

    }

     mysqli_close($link);

    ?>

Note: For any help write in comments.

Post a Comment

4 Comments

  1. How to create database help me

    ReplyDelete
    Replies
    1. see the below link for complete details.

      http://p4programmingland.blogspot.in/2016/01/insert-data-into-database-using-php.html

      Delete
  2. Sir can you provide me search coding

    ReplyDelete
  3. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks odzyskiwanie danych z uszkodzonego telefonu

    ReplyDelete