mysqli从数据库获取信息

时间:2014-01-22 09:47:53

标签: php mysql mysqli

尝试从数据库中获取信息到表中。下面是我正在使用的代码,但它没有填充表格。该网站出现但数据库中没有信息。请帮助,我对这个php事物很新,并且不知道我在做什么,除了谷歌!

    <body>
 <?php include("header.php"); ?>
<?php
$con=mysqli_connect("localhost","username","password","database");
 // Check connection
 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>";

mysqli_free_result($result);
mysqli_close($con);
?>
</div>
</body>

3 个答案:

答案 0 :(得分:2)

您的查询错误

$query = "SELECT * FROM 'teacher'";

应该是

$query = "SELECT * FROM `teacher`";

答案 1 :(得分:1)

"SELECT * FROM teacher"

只需删除引号即可。

答案 2 :(得分:0)

试试这个

$query = "SELECT * FROM teacher";