PHP CRUD : How to Fetch data from Database in php

Fetch data from Database in php


Hie Guys, As we are going to start PHP CRUD Application where we are going to start with fetching data from database in php.

So, you will learn how to fetch data from database in php mysql. First create a table into your database as given in STEP 1. and feed some data in your table, so we can retrieve the data from mysql table.

So guys, Let's get started: 

Step 1: Create a database phpcrud and in that create a table named student :

CREATE TABLE student (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fname VARCHAR(30) NOT NULL,
    lname VARCHAR(30) NOT NULL,
    contact VARCHAR(50) NOT NULL,
)

Step 2: Create a file named index.php and we will use bootstrap to design the table and fetch data into that html table code given below:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <title>PHP CRUD - Funda of Web IT</title>
</head>
<body>
    <div class="container">
        <div class="jumbotron">

            <div class="row">
                <div class="col-md-6">
                    <h2>PHP - CRUD : Display Data in PHP  </h2>
                </div>
                <div class="col-md-6">
                    <a href="insertdata.php" class="btn btn-success" style="margin-left: 80%;"> ADD DATA </a>    
                </div>
                <div class="col-md-12">
                    <hr>
                </div>
            </div>

            <?php
                $connection = mysqli_connect("localhost","root","");
                $db = mysqli_select_db($connection'phpcrud');

                $query = "SELECT * FROM students";
                $query_run = mysqli_query($connection$query);
            ?>

            <div class="row">
                <div class="col-md-12">
                    <table class="table table-bordered" style="background-color: white;">
                        <thead class="table-dark">
                            <tr>
                                <th> ID </th>
                                <th> First Name </th>
                                <th> Last Name </th>
                                <th> Contact </th>
                                <th> EDIT </th>
                                <th> DELETE </th>
                            </tr>
                        </thead>
                        <tbody>
                                        
                        <?php
                        if($query_run)
                        {
                            while($row = mysqli_fetch_array($query_run))
                            {
                                ?>
                                    <tr>
                                        <th> <?php echo $row['id']; ?> </th>
                                        <th> <?php echo $row['fname']; ?> </th>
                                        <th> <?php echo $row['lname']; ?> </th>
                                        <th> <?php echo $row['contact']; ?> </th>
                                        <th> 
                                            <form action="updatedata.php" method="post">
                                                <input type="hidden" name="id" value="<?php echo $row['id'?>">
                                                <input type="submit" name="edit" class="btn btn-success" value="EDIT">
                                            </form>
                                        </th>
                                        <th> 
                                            <form action="delete.php" method="post">
                                                <input type="hidden" name="id" value="<?php echo $row['id'?>">
                                                <input type="submit" name="delete" class="btn btn-danger" value="DELETE"> 
                                            </form>
                                        </th>
                                    </tr>
                                <?php
                                }
                            }
                            else
                            {
                                ?>
                                    <tr>    
                                        <th colspan="6"> No Record Found </th>
                                    </th>
                                <?php
                            }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</body>
</html>


Thanks for reading...