gpt4 book ai didi

PHP 查询不输出到表中

转载 作者:行者123 更新时间:2023-11-30 22:30:26 25 4
gpt4 key购买 nike

所以我有一个查询,它从一个或两个字段中提取大部分来自 mysql 数据库的信息。我想将数据输出到表格中,但由于数据逐行输出以合并 html 表格标签的方式,我一直在苦苦挣扎。一些 friend 建议使用 CSS 在各个列周围设置边框样式,但我不想使用 set width css borders 来制作表格。

到目前为止,在“while”之后回显表格的任何尝试都将非常有用,这会导致每行数据之间出现一个 1px x 1px 的表格。

 <?php  include 'database_conn.php';      // make db connection

$sql = "SELECT CDID, CDTitle, CDYear, catID, CDPrice FROM `nmc_cd` ORDER BY `nmc_cd`.`CDTitle` ASC";

$rsCD = mysqli_query($conn, $sql) or die(mysqli_error($conn));

while ($row = mysqli_fetch_assoc($rsCD)) {
$CDID = $row['CDID'];
$CDTitle = $row['CDTitle'];
$CDYear = $row['CDYear'];
$catID = $row['catID'];
$CDPrice = $row['CDPrice'];

echo "<br>";
echo "<span class= \"CDID\">$CDID</span>\n";
echo "<span class= \"CDTitle\">$CDTitle</span>\n";
echo "<span class= \"CDYear\">$CDYear</span>\n";
echo "<span class= \"catID\">$catID</span>\n";
echo "<span class= \"CDPrice\">$CDPrice</span>\n";
echo "</div>\n";

} mysqli_free_result($rsCD);
mysqli_close($conn); ?>
  • 更新说明 *尝试了几种不同的方法,下面是其中一种。

    $sql = "SELECT CDID, CDTitle, CDYear, catID, CDPrice FROM `nmc_cd` ORDER BY `nmc_cd`.`CDTitle` ASC";

    $rsCD = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    while ($row = mysqli_fetch_assoc($rsCD)) {
    $CDID = $row['CDID'];
    $CDTitle = $row['CDTitle'];
    $CDYear = $row['CDYear'];
    $catID = $row['catID'];
    $CDPrice = $row['CDPrice'];

    echo "<table id='dattabletho'>";
    echo"<tr>";
    echo "<td><span class= \'CDID\'> " . $CDID . "</span>\n";
    echo "<td><span class= \'CDTitle\'> " . $CDTitle . "</span></td>\n";
    echo "<td><span class= \'CDYear\'> " . $CDYear . "</span></td>\n";
    echo "<td><span class= \'catID\'> " . $catID . "</span></td>\n";
    echo "<td><span class= \'CDPrice\'> " . $CDPrice . "</span></td>\n";
    echo"</tr>";

    }
    echo "</table>";
    mysqli_free_result($rsCD);
    mysqli_close($conn); ?>

通过上面的代码,我设法让每一行输出为一个联合行,我试图将查询中的数据放入一个表中,而不是输出一个简单的空表。这很困难的原因是因为我需要将要检索的几条记录放在同一行上,并且最好在表中对齐列。我知道这与 PHP 问题一样都是 HTML 问题我对 PHP/SQL 还很陌生。

最佳答案

<?php  include 'database_conn.php';      // make db connection

$sql = "SELECT CDID, CDTitle, CDYear, catID, CDPrice FROM `nmc_cd` ORDER BY `nmc_cd`.`CDTitle` ASC";

$rsCD = mysqli_query($conn, $sql) or die(mysqli_error($conn));

while ($row = mysqli_fetch_assoc($rsCD)) {
$CDID = $row['CDID'];
$CDTitle = $row['CDTitle'];
$CDYear = $row['CDYear'];
$catID = $row['catID'];
$CDPrice = $row['CDPrice'];

echo "<br>";
echo "<span class= \'CDID\'> " . $CDID . "</span>\n";
echo "<span class= \'CDTitle\'> " . $CDTitle . "</span>\n";
echo "<span class= \'CDYear\'> " . $CDYear . "</span>\n";
echo "<span class= \'catID\'> " . $catID . "</span>\n";
echo "<span class= \'CDPrice\'> " . $CDPrice . "</span>\n";
echo "</div>\n";

} mysqli_free_result($rsCD);
mysqli_close($conn); ?>

关于PHP 查询不输出到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34035555/

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