当前位置: 动力学知识库 > 问答 > 编程问答 >

html - PHP Query not Outputting into a Table

问题描述:

So I have a query that is pulling most the info from a mysql db apart from one or two fields. I want to output the data into a table but I have been struggling due to the way the data is outputting line by line to incorporate html table tags. Some friends suggested using CSS to style borders around the various columns but I don't want to use set width css borders to make a table.

Any assistance would be great so far any attempt to echo a table after the "while" causes a 1px x 1px table between every line of data.

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

  • UPDATE CLARIFICATION *Tried a few different methods one of which is below.

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

With the above code I have managed to get each line outputting as a joint row, I am trying to get the data from my query into a table not output a simple empty table. The reason this is difficult is because I need several records that are being retrieved to be on the same row and preferably aligned columns in the table. I am aware this is just as much a HTML question as PHP question I am quite new to 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); ?>
网友答案:

First we have include config file means we have connect database

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

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: