【问题标题】:Creating a table in HTML using data from MySQL Database not working使用 MySQL 数据库中的数据在 HTML 中创建表不起作用
【发布时间】:2016-12-09 18:57:11
【问题描述】:

我正在尝试在来自 MySQL 数据库的网页上显示一个表格,但它现在可以工作了!这是我的代码:

<?php 
    function list_schools() {
       $conn = mysqli_connect("localhost", "root", "", "Database_Project");
       if ($conn === false) {
          die("Could not connect:"  . mysqli_connect_error()); 
       } 

       $output = "";
       $result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');       
       while ($row = mysqli_fetch_array($result)){
          $output .= '
          <tr>
          <td>' . $row['school_id'] . '</td>
          <td>' . $row['name'] . '</td>                                                   
          <td>' . $row['address'] . '</td>
          <td>' . $row['phone_number'] . '</td>
          <td>' . $row['email'] . '</td> 
          <td>' . $row['type'] . '</td>                                    
          </tr>';
       }
       return $output;

    }

    $exec_func = list_schools();
?>

<table cellpadding="0" cellspacing="0" width="100%" class="sortable">
    <thead>
       <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Address</th>
          <th>Phone Number</th>
          <th>Email</th>
          <th>Type</th>
          <td>&nbsp;</td>
       </tr>
    </thead>
    <tbody>
       <?php echo exec_func; ?>
    </tbody>
</table>

每当我尝试在浏览器上显示它时,我都会得到以下信息:

1

无法显示来自 1 的列

我做错了什么?

【问题讨论】:

  • 您选择的是school_id,而不是ID
  • 我更正了,但仍然没有表格显示@aynber
  • 另外,exec_func 是什么?如果您尝试回显返回变量,则会丢失美元符号。
  • @aynber 不,恐怕不是这样;还是不行
  • 您是否仍然收到“无法显示来自 1 的列”或其他消息?现在屏幕上显示的是什么?

标签: php mysql html apache


【解决方案1】:

从您的查询中,很明显您不会得到ID 列,而是school_id

因此,只需在构建 $output 时更正您的陈述:

     <td>' . $row['school_id'] . '</td>

【讨论】:

  • @thelili018 但这有效地解决了您在 OP 中显示的问题。现在这是一个不同的问题!
【解决方案2】:

为什么不试试:

<?php 
    function list_schools() {
       $conn = mysqli_connect("localhost", "root", "", "Database_Project");
       if ($conn === false) {
          die("Could not connect:"  . mysqli_connect_error()); 
       } 

       $output = "";
       $result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');       



    }

    $exec_func = list_schools();
?>

<table cellpadding="0" cellspacing="0" width="100%" class="sortable">
    <thead>
       <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Address</th>
          <th>Phone Number</th>
          <th>Email</th>
          <th>Type</th>
          <td>&nbsp;</td>
       </tr>
    </thead>
    <tbody>
       <?php while ($row = mysqli_fetch_array($result)){
          echo '
          <tr>
          <td>' . $row['school_id'] . '</td>
          <td>' . $row['name'] . '</td>                                                   
          <td>' . $row['address'] . '</td>
          <td>' . $row['phone_number'] . '</td>
          <td>' . $row['email'] . '</td> 
          <td>' . $row['type'] . '</td>                                    
          </tr>'; } ?>
    </tbody>
</table>

【讨论】:

    【解决方案3】:

    您可以使用以下代码

       <table cellpadding="0" cellspacing="0" width="100%" class="sortable">
        <thead>
           <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Address</th>
              <th>Phone Number</th>
              <th>Email</th>
              <th>Type</th>
              <td>&nbsp;</td>
           </tr>
        </thead>
        <tbody>
       <?php 
           $conn = mysqli_connect("localhost", "root", "", "Database_Project");
           if ($conn === false) {
              die("Could not connect:"  . mysqli_connect_error()); 
           } 
    
            $results = $mysqli->query("SELECT school_id, name, address, phone_number, email, type FROM Schools");
            while($obj = $results->fetch_object()) { 
             ?>
              <tr>
              <td> <?php echo $row['school_id']; ?> </td>
              <td> <?php echo $row['name']; ?></td>                                                   
              <td> <?php echo $row['address']; ?></td>
              <td> <?php echo $row['phone_number']; ?></td>
              <td> <?php echo $row['email']; ?></td> 
              <td><?php echo $row['type']; ?></td>                                    
              </tr>
          <?php
           }
    ?>
    
    
        </tbody>
    </table>
    

    【讨论】:

      【解决方案4】:

      你的问题在这里:

      $result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');
      

      您不能在此处使用or 运算符。 检查此Logical Operators 以获得解释。

      示例:

      echo 't' or 'f'; //will output 1
      

      使用此代码:

      $result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;")
      $num_rows = mysqli_num_rows($result);
      if (!$num_rows) {
      die('cannot show tables');   
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-03
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-09
        • 2020-11-10
        • 1970-01-01
        • 2016-09-09
        相关资源
        最近更新 更多