Database Problem - Can't populate to table

Hi guys,

Can you check this code and see if there is something that is preventing the table from being populated?

<?php include("header.php"); ?> <?php $con=mysqli_connect("localhost","username","password","databasename"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$query = "SELECT * FROM 'teacher'";
$result = mysqli_query($con, $query);

echo "<div align=\"center\">";
echo "<table width=\"100%\">";
echo "<tr>";
echo "<th>First Name</th>";
echo "<th>Middle Name</th>";
echo "<th>Last Name</th>";
echo "</tr>";

while ($row = mysqli_fetch_array($result)) 
echo "<tr>";
echo "</td><td>";
echo $row['first_name'];
echo "</td><td>";
echo $row['middle_name'];
echo "</td><td>";
echo $row['last_name'];
echo "</td></tr>";
echo "</table>";


You have a closing tag before opening tag on the second line of the where loop but the code for mysqli looks fine to me.

Add some error handling to your query as well, your syntax is invalid

’ <- this quote != this quote -> `

’ is used to quote strings
`is used around db/table/column names

`is not actually needed, unless you’re using reserved words (select, user, limit, order, etc). which you are not.

Either changing to teachersor just removing the quotes alltogether should give you results.

Good catch Batman!!

I just glanced through an my minds eye saw backticks!!
Note to self: Must do better!! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service