Retrieve data from mysql table and display in php

The common and basic module where we require in php programming is to retrieve data from database and show in html page. Lets follow the basics steps for demostration

  • create basic data table in mysql by using phpmyadmin
  • create php database connection file to the database
  • write php code to display data in html table
  • post image

    create a folder inside htdocs folder, with a name you like and create two files with the name of index.php and connection.php. connection file content would be as follows

    
    $conn=mysqli_connect(“localhost“,“root“,““,“mydb“);
    if (mysqli_connect_errno()) {
      echo “Failed to connect to MySQL: “ . mysqli_connect_error();
    }
    
    connection format:mysqli_connect("HOST","USER","PASSWORD","DATABASE");

    lets write following content inside index.php file

    
    
    <table>
        <thead>
            <td>USER ID</td>
            <td>NAME</td>
            <td>AGE</td>
        </thead>
        <tbody>
        <?php
        include('connection.php');
        $result=mysqli_query($conn,"SELECT * FROM mytable");
        while($user=mysqli_fetch_assoc($result))
        {
        ?>
        <tr>
            <td><?php echo $user['userId']; ?></td>
            <td><?php echo $user['name']; ?></td>
            <td><?php echo $user['age']; ?></td>
        </tr>
        <?php
        }
        ?>
        </tbody>
    <table>
    

    you will see the following output from the browser if you visit localhost/phptest/

    post image

    Rasika Preethiraj

    A technophile at icrony since 2012, In a quest to programmatic automations for scaled organisations through the use of python, php, autoit and machine learning


    Comments


    Post a Comment



    Trending

    Latest Posts

    Tags

    Newsletter

    Subscribe to our newsletter for good news, sent out every month.