Laurent Coulon
Laurent Coulon

Reputation: 109

Why mySQL table won't export properly as HTML table

I use bootstrap 3 template and connect to a mysql database, sort the table and export some datas as HTML table in order to display it on bootstrap webpage.

Here is the mySQL look like : enter image description here

The following code won't get expected 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($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()
        . ")");
}
?>

original page link can be found here : http://s529471052.onlinehome.fr/bs3/gpio/dyntable.htm

Upvotes: 0

Views: 74

Answers (3)

bayodesegun
bayodesegun

Reputation: 1

I'm assuming that your connection to the database is OK (though the given page link suggests otherwise).

It seems the table is not exporting properly because $result was misspelled as $results in the code segment printing the data rows: $row = mysqli_fetch_array($results).

Try the modification below (also refactored for better readability):

<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 -->

Also, you may want to check that $results and $row contain the expected data in the expected formats using print_r($results) and print_r($row).

Upvotes: 0

Mukesh
Mukesh

Reputation: 150

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

Try this, This will help for your requirement.

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

Its just a simple typo

$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)) {

While testing Add

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

to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser.

Answer for additional info provided in a comment

From your comment below, it appears you are trying to run PHP code from a web page with a .htm extension. That wont work unless you have configured Apache to do this

Change the web page file name to have a .php extension and then Apache will pass the PHP code to the PHP interpreter for compilation and execution.

Upvotes: 2

Related Questions