为什么mySQL表不能像HTML表那样正确导出

时间:2017-07-05 12:10:28

标签: php html mysql html-table

我使用bootstrap 3模板连接到mysql数据库,对表进行排序并将一些数据导出为HTML表格,以便在bootstrap网页上显示。

这是mySQL的样子: enter image description here

以下代码未获得预期结果:

<div class="well">              

<?php               

//MySQL Database Connect
include '/includes/dbconnect.php';

$result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");

if(mysqli_num_rows($result) >= 1) {

    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>"; 
    while($row = mysqli_fetch_array($results)) {
        echo "<tr><td>"; 
        echo $row['ID'];
        echo "</td><td>";   
        echo $row['NAME'];
        echo "</td><td>";    
        echo $row['NEXT_EVENT'];
        echo "</td></tr>";  
    }
    echo "</table>";  
}
?>
</div>  <!-- well -->

dbconnect.php:

<?php

$localhost="xxx.xx.xxx.com";
$username="dboxxxxxxx";
$password="xxxxxx";
$database="dbxxxxxx";

$conn = mysqli_connect($localhost,$username,$password,$database);

//test if connection failed
if(mysqli_connect_errno()){
    die("connection failed: "
        . mysqli_connect_error()
        . " (" . mysqli_connect_errno()
        . ")");
}
?>

原始页面链接可在此处找到:http://s529471052.onlinehome.fr/bs3/gpio/dyntable.htm

3 个答案:

答案 0 :(得分:2)

这只是一个简单的错字

$result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");

if(mysqli_num_rows($result) >= 1) {

    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>"; 
    //while($row = mysqli_fetch_array($results)) {
                                      ^^^^^^^^
    while($row = mysqli_fetch_array($result)) {
  

测试添加

ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); `
  

到你的脚本的顶部。这将强制执行任何mysqli_错误   生成您可以在浏览器上看到的异常,并且您的浏览器上也会显示其他错误。

回答评论中提供的其他信息

从下面的评论中,您似乎正在尝试从具有.htm扩展名的网页运行PHP代码。 除非您已将Apache配置为执行此操作,否则无法正常工作

将网页文件名更改为.php扩展名,然后Apache将PHP代码传递给PHP解释器以进行编译和执行。

答案 1 :(得分:0)

$result = mysqli_query($conn,"SELECT ID, NAME, NEXT_EVENT  FROM webpilot ORDER BY NEXT_EVENT ASC");

$table = '<table>
            <tr>
                <th>ID</th>
                <th>NAME</th>
                <th>EPOCH</th>
            </tr>';
if($result ) {
    while ($row = mysqli_fetch_assoc($result)) {
        $table .= '<tr>
                    <td>'. $row['ID'] .'</td>
                    <td>'. $row['NAME'] .'</td>
                    <td>'. $row['NEXT_EVENT'] .'</td>
                   </tr>';
    }
} else {
    $table .= '<tr><td colspan="3">No date found</td></tr>';
}
$table .= '</table>';
echo $table;

试试这个,这将有助于您的要求。

答案 2 :(得分:0)

我假设你与数据库的连接是正常的(尽管给定的page lin k建议不然)。

由于$result在打印数据行的代码段中$results拼写错误,似乎表格未正确导出:$row = mysqli_fetch_array($results)

尝试下面的修改(也为了更好的可读性而重构):

<div class="well">              

<?php               

  //MySQL Database Connect
  include '/includes/dbconnect.php';

  $result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");

  if(mysqli_num_rows($result) >= 1)
  {
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>"; 
    while($row = mysqli_fetch_array($result))
    {
      echo "<tr>";
      echo "<td>$row['ID']</td>";           
      echo "<td>$row['NAME']</td>";                       
      echo "<td>$row['NEXT_EVENT']</td>";                         
      echo "</tr>";  
    }
    echo "</table>"; 
  }

?>
</div>  <!-- well -->

此外,您可能需要使用$results$row检查print_r($results)print_r($row)是否包含预期格式的预期数据。