【问题标题】:Displaying columns in PHP [duplicate]在 PHP 中显示列 [重复]
【发布时间】:2022-01-25 22:15:29
【问题描述】:

有人知道如何得到这个结果吗?基本上我也只需要显示列。
Wanted Result image

Current result image


   <h1>
        Harry Potter Movies
    </h1>
    
<?php
     $hostname = "localhost"; 
     $dbUser = "root"; 
     $dbPassword = "root"; 
     $dbName = "harry potter movies";
     $port = 3306;

     $conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);

     $sql = "SELECT * FROM Movies";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
       
        while($row = $result->fetch_assoc()) {
        
          echo $row["movie"]. "   " . $row["rating"]. "   " . $row["year"]. "<br>" ;
        }
        
      } else {
        echo "0 results";
      }

   ?> 

【问题讨论】:

标签: php html


【解决方案1】:

您可以为此使用表格。

<?php
$hostname = "localhost"; 
$dbUser = "root"; 
$dbPassword = "root"; 
$dbName = "harry potter movies";
$port = 3306;

$conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);

if (mysqli_connect_errno()) exit("Failed to connect to MySQL: " . mysqli_connect_error());
?>

<h1>Harry Potter Movies</h1>
    
<table>
    <thead>
        <tr>
            <th>Movie</th>
            <th>Rading</th>
            <th>Year</th>
        </tr>
    </thead>
    <tbody>
    <?php 
    $result = $conn->query("SELECT movie, rating, year FROM Movies");
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
        ?>
        <tr>
            <td><?= htmlentities($row["movie"], ENT_QUOTES) ?></td>
            <td><?= htmlentities($row["rating"], ENT_QUOTES) ?></td>
            <td><?= htmlentities($row["year"], ENT_QUOTES) ?></td>
        </tr>
        <?php
        }
    } else { ?>
        <tr>
            <td colspan="3">0 results</td>
        </tr>
        <?php
    }
    ?>
    </tbody>
</table>

【讨论】:

    【解决方案2】:

    你想要的是这样的:

    <table>
     <tr>
       <th>
         Movie
       </th>
       <th>
         Rating
       </th>
       <th>
         Year
       </th>
     </tr>
     <tr>
       <td>
         Harry Potter and the Philosopher's Stone
       </td>
       <td>
         7.6
       </td>
       <td>
         2001
       </td>
     </tr>
    

    所以你的回声应该用内容输出一些 HTML 标记。

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多