PHP查询未输出到表中

时间:2015-12-02 05:28:41

标签: php html mysql sql

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

到目前为止,在""之后任何尝试回复表格的任何帮助都会很棒。在每行数据之间产生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很新。

2 个答案:

答案 0 :(得分:0)

<?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); ?>

答案 1 :(得分:0)

首先我们包含配置文件意味着我们有连接数据库

$query="select * from city";
$res = mysql_query($query);
while($data=mysql_fetch_array(res)){
$data['city_name']."<br>";

}