输出-6ren">
gpt4 book ai didi

php - Mysql fetch与PHP MYSQL以表格式关联

转载 作者:行者123 更新时间:2023-11-29 08:05:33 24 4
gpt4 key购买 nike

我有这样的代码。

代码:

        <?php
$book_query = mysql_query("select * from book_master')");
while($book_query_fetch = mysql_fetch_assoc($book_query)){
echo "<pre>";
print_r($book_query_fetch);
echo "</pre>"
}
?>

输出:

        Array
(
[Book_Name] => Book1
[Book_ID] => 123
)

Array
(
[Book_Name] => Book2
[Book_ID] => 124
)

预期输出:(在表格中)

        Book Name       Book_ID
Book1 123
Book2 124

我怎样才能实现这个目标?编辑: header 部分是动态负载。所以我也需要循环中的表头

最佳答案

我不知道你在哪里坚持这样做,但你可以在下面这样做,

 echo "<table>";
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
if($i == 0){
$columns = array_keys($row);
echo "<th>";
foreach($columns as $column){
echo "<td> $column</td>";
}
echo "</th>";
}
echo'<tr>';
echo '<td>'.$row['Book_Name'].'</td>';
echo '<td>'.$row['Book_ID'].'</td>';
echo '</tr>';
$i++;
}
echo "</table>";

华林: Please, don't use mysql_* functions in new code 。它们不再维护and are officially deprecated 。请参阅red box ?了解 prepared statements相反,并使用 PDO ,或MySQLi -this article将帮助您决定哪个。如果您选择 PDO,here is a good tutorial .

关于php - Mysql fetch与PHP MYSQL以表格式关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22784349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com