简体   繁体   中英

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.

<? echo $customerID;  ?>

应该

<?php echo $customerID;  ?>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM