简体   繁体   English

PHP 和 Mysql while 循环

[英]PHP and Mysql while loop

I have a mysql table with orders.我有一个带有订单的 mysql 表。 I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts.我试图遍历订单表并根据他们的用户 ID 选择单个客户订单并在他们的客户帐户上显示订单。 However, my code below just prints the first row and repeats it endlessly jaming my browser every time.但是,我下面的代码只是打印第一行,并且每次都无休止地重复它干扰我的浏览器。

what is wrong with this and how to i solve it这有什么问题以及如何解决

<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
 $records-> bindParam(':id', $_SESSION['user_id']);
 $records->execute();
 $results=$records->fetch(PDO::FETCH_ASSOC);

 $i=0;
    while($i<=$results):    

        $i++;
 ?> 
<h3>Your Orders</h3>
<table >
    <tr >
        <th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
    </tr>
    <tr>
        <td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>

    </tr>
</table>
<?php
endwhile;
?>

Remove all $i related code.删除所有与$i相关的代码。 Just move your fetch statement to while condition , like the following:只需将您的fetch语句移动到while condition ,如下所示:

while( $results=$records->fetch(PDO::FETCH_ASSOC))

You need to include the html code in the while loop because you want to display all of the orders.您需要在 while 循环中包含 html 代码,因为您要显示所有订单。

I'm using this method, try this out.我正在使用这个方法,试试这个。

Start while in first php section在第一个 php 部分开始

<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();

   while($results=$sql->fetch(PDO::FETCH_ASSOC)){

?> 

After that the html section coming:之后是 html 部分:

<h3>Your Orders</h3>
<table >
    <tr >
        <th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
    </tr>
    <tr>
        <td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>

    </tr>
</table>

and here the ending这里是结局

<?php
}
?>

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

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