I am extremely new to PHP and I'm frustrated by the error of this simple task. I want to import a table from an SQL database and show it in a HTML table. But I keep getting errors when trying to fetch the table column names.
The connection with the database is made though, I tested that.
The error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table' at line 1.
I found this example on w3schools which I edited by other examples on php.net
If anybody can help me with this, I'd appreciate it.
<?php
error_reporting(-1);
$con = mysqli_connect('localhost', 'user', 'pass');
mysqli_select_db($con, 'database') or die("Could not found" . mysqli_error($con));
$query = ("select * from Table");
$result = mysqli_query($con, $query) or die ( mysqli_error ($con) );
//Print table
echo "<table>";
echo "<tr>";
if($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
//Print headers
foreach($row as $key => $value ){
echo "<td>" . $key . "</td>";
}
echo "</tr>";
}
$result = mysqli_query($con, $query) or die (mysqli_error());
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
while( list($key, $value) = each($row)){
//Print value
echo "<td>" . $value . "</td>";
}
echo "<td>" . $value . "<i class='fa fa-caret-up'></i><i class='fa fa-caret-down'></i></td>";
echo "</tr>";
}
echo "</table>";
?>