dongyong9224 2016-03-29 15:05
浏览 42

将数据库中的多行打印到表中

Could anyone tell me how to adapt or change this code to print multiple rows from a database table into an html table using PHP? Every user will have a different number of records in the table so I will need to use some kind of loop to print out the rows.

<?php

                    include_once 'dbconfig.php';

                    $statement = $db_con->prepare("SELECT * FROM entry WHERE user_id=:uid");
                    $statement->execute(array(":uid"=>$_SESSION['user_session']));
                    $result=$statement->fetch(PDO::FETCH_ASSOC);

                    ?>

                    <table>
                        <tr>
                        <th>Entry</th>
                            <th>Date</th>
                            <th>Weight</th>
                            <th>BMI</th>
                            <th>Calories Consumed</th>
                            <th>Calories Burned</th>
                            <th>Calorific Deficit</th>
                        </tr>

                    <?php

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

        $entry = $row["entry_id"];           
        $date = $row["date"]; 
        $weight = $row["weight"];
        $bmi = $row["bmi"]; 
        $consumed = $row["calories_consumed"]; 
        $burned = $row["calories_burned"];
        $deficit = $row["calorie_deficit"]; 

        echo "<tr>
        <td>$entry</td>
        <td>$date</td>
        <td>$weight</td>
        <td>$bmi</td>
        <td>$consumed</td>
        <td>$burned</td>
        <td>$deficit</td>
        </tr>";

}

                    ?>

                        </table>

This currently works fine but it only prints out the first row relevant to the particular user rather than looping through and printing a row for each record.

Updated to include while loop

  • 写回答

1条回答 默认 最新

  • dongwu1992 2016-03-29 15:20
    关注
    while ($row =$statement->fetch(PDO::FETCH_ASSOC)) {
                $title1 = $row["title1"]; 
                $title2 = $row["titl2"]; 
                $title3 = $row["title3"]; 
    
                echo "<tr> 
                <td>$title1</td>
                <td>$title2</td>
                <td>$title3</td>
                 </tr>";
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错