Learn PHP Basics: Master CRUD Operations to Build Dynamic Applications
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Notes</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td> record id</td>
            <td> full name</td>
            <td> email address</td>
            <td> phone number</td>
            <td> notes</td>
            <td></td>
        </tr>
        </tbody>
    </table>

    5. Show data (Read) 

    Let’s modify our index.php. The following code loops through the data in our database and structures it in a table on our webpage.

    <?php 
        include('connect.php');
        include('head.php'); 
    ?> 
        <h1>Contact Manager</h1> 
        <a href="create.php">Create New Contact</a>
        <table border="1">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Notes</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody> 
            <?php // retrieving data from database
            
            $sql = "SELECT * FROM contacts";
            $row = $connection->prepare($sql); 
            $row->execute(); 
            $contacts = $row->fetchAll(); 
            
            // showing the data with a loop 
            ?> 
            <?php foreach ($contacts as $contact) { ?>
                <tr>
                    <td><?php echo $contact['id']; ?></td>
                    <td><?php echo $contact['name']; ?></td>
                    <td><?php echo $contact['email']; ?></td>
                    <td><?php echo $contact['phone']; ?></td>
                    <td><?php echo $contact['notes']; ?></td>
                    <td> 
                        <!-- redirecting to edit page with the id as identifier --> 
                        <a href="edit.php?id=<?php echo $contact['id']; ?>">Edit</a>
                        
                        <!-- deleting a data with id as identifier --> 
                        <a onclick="return confirm('Delete this contact?')" href="delete.php?id=<?php echo $contact['id']; ?>">Delete</a>
                    </td>
                </tr> 
            <?php } ?> 
            </tbody>
        </table> 
    <?php include('foot.php') ?>

    Break it down

    Read part has to do with displaying the records, that we entered into the database, to the screen. The following code will create a render our data in a nicely structured table with rows, columns and column titles.

    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Notes</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td> record id</td>
            <td> full name</td>
            <td> email address</td>
            <td> phone number</td>
            <td> notes</td>
            <td></td>
        </tr>
        </tbody>
    </table>

    Once we have finished our table structure, we need to fill it with data. We do this by surrounding our tr (table row) with a foreach loop that will loop over the data in the database and for each record that is found in the database, a row will be printed to the screen.

    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Notes</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody> 
        <?php 
            // retrieving data from database 
            $sql = "SELECT * FROM contacts"; 
            $row = $connection->prepare($sql); $row->execute();       
            $contacts = $row->fetchAll(); 
        
            // showing the data with a loop 
            foreach($contacts as $contact){ 
        ?>
        <tr>
            <td><?php echo $contact['id']; ?></td>
            <td><?php echo $contact['name']; ?></td>
            <td><?php echo $contact['email']; ?></td>
            <td><?php echo $contact['phone']; ?></td>
            <td><?php echo $contact['notes']; ?></td>
            <td> 
    
            <!-- redirecting to edit page with the id as identifier --> 
            <a href="edit.php?id=<?php echo $contact['id']; ?>">Edit</a>
            <!-- deleting a data with id as identifier --> 
            <a onclick="return confirm('Delete this contact?')" href="delete.php?id=<?php echo $contact['id']; ?>">Delete</a>
            </td>
        </tr> 
        <?php } ?> 
        </tbody>
    </table>

    Index page with data