繁体   English   中英

我如何使用PHP将SQL属性回显到HTML表

[英]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>

我正在学习PHP,因此需要尽快获得帮助。

当我运行网页时,除了表头之外,什么都没有显示到表中。 请帮忙,因为我正在学习PHP。 我正在使用xampp创建数据库。

<? echo $customerID;  ?>

应该

<?php echo $customerID;  ?>

首先

不要使用mysql_*函数 ,它们不再维护并且已正式弃用 改为了解准备好的语句 ,并使用PDOMySQLi 本文将帮助您做出决定。

但是要回答您的问题,您需要像这样在while循环中输出表数据:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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