Willie Mwewa
Willie Mwewa

Reputation: 361

How can i echo sql attributes to an HTML table using PHP

  <div class="col-md-6">
                    <?php
                        include 'includes/connection.php';

                        $query = "SELECT * FROM customer";

                        $result = mysql_query($query);


                        while ($person = mysql_fetch_array($result)) {

                            //echo "<p>" . $person['customerID'] . "</p>";
                            //echo "<p>" . $person['firstName'] . "</p>";
                            //echo "<p>" . $person['lastName'] . "</p>";
                            //echo "<p>" . $person['address'] . "</p>";

                            $customerID  = $person['customerID'];
                            $firstName   = $person['firstName'];
                            $lastName    = $person['lastName'];
                            $address     = $person['address'];

                            echo $customerID; //A test echo that is working        just fine. But i need to echo this to the table
                        }




                        ?>
                        <table class="table table-striped">
                            <tr>
                                <th>Customer ID</th>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Address</th>
                            </tr>    
                            </th>
                            <tr>
                                <td> 
                                    <? echo $customerID;  ?>
                                </td>
                                <td>
                                    <? echo $firstName; ?>
                                </td>
                                <td>
                                    <? echo $lastName; ?>
                                </td>
                                <td> 
                                    <? echo $address; ?>
                                </td>
                            </tr>

                        </table>


    </div>
</div>

I am learning PHP and i need help with this as soon as possible.

When i run the web page nothing is being shown to the table apart from the table headers. Please help for i am learning PHP. I am using xampp on which i created a database.

Upvotes: 0

Views: 1041

Answers (2)

Michael Doye
Michael Doye

Reputation: 8171

Firstly:

Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. This article will help you decide.

But to answer your question, you need to output your table data inside of your while loop like this:

<table class="table table-striped">
      <tr>
        <th>Customer ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Address</th>
     </tr>    
     </th>
     <?php while ($person = mysql_fetch_array($result)) { ?>
     <tr>
        <td> 
        <?php echo $person['customerID'];  ?>
        </td>
        <td>
        <?php echo $person['firstName']; ?>
        </td>
        <td>
        <?php echo $person['lastName']; ?>
        </td>
        <td> 
        <?php echo $person['address']; ?>
        </td>
    </tr>
   <?php } ?>
</table>

Upvotes: 2

Eric Lamas
Eric Lamas

Reputation: 129

<? echo $customerID;  ?>

should be

<?php echo $customerID;  ?>

Upvotes: 2

Related Questions