我用他的php mysql做错了什么

时间:2016-04-28 03:19:26

标签: php mysql

我的目的是在我的网站上放一个包含来自数据库中包含ID的字段的表

$con = mysqli_connect('hostname','username','password','dbname');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}



$sql="SELECT * FROM Orders WHERE `Student UID` = '".$id."'";
$result = mysqli_query($con,$sql);
echo 
echo "<table><tr><th>Product</th><th>Cost</th></tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['MenuItemName'] . "</td>";
    echo "<td>" . $row['Price'] . "</td>";


    echo "</tr>";
}
echo "</table>";

在我运行这个时,我正突然得到一张像这样的桌子

+---------+-------+
| Product | Price |
+---------+-------+

我的数据库看起来像这样

+--------------+-------+-------------+
| MenuItemName | Price | Student UID |
+--------------+-------+-------------+
| Foo          |    99 |       12345 |
| Foo2         |    11 |       11111 |
| Foo2         |    11 |       12345 |
+--------------+-------+-------------+

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试更改此行:

$sql="SELECT * FROM Orders WHERE 'Student UID' = '".$id."'";

为:

$sql="SELECT * FROM Orders WHERE `Student UID` = '".$id."'";

喊出来解决你的问题。