我正在尝试为我的一个小客户拼凑一个简单的库存数据库(我通常不做WebDev的东西),但我有点难过。我有我认为应该工作的东西,但我的表格没有结果。我知道查询是好的,因为我在直接查询数据库时得到了预期的结果,除非PHP期望我的SQL语句的格式不同。这是我的页面:
<html>
<head>
<title>Inventory</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT
products.name,
products.sku,
inventory.quantityfry,
inventory.quantityjuv,
inventory.quantityadult,
inventory.notes,
inventory.location,
inventory.owner
FROM
products
INNER JOIN
inventory
ON
products.sku=inventory.sku";
$result = mysqli_query($query);
echo "<table border='1'>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
当我加载页面时,我看到的只是我的HTML表格标题,但没有数据。也没有错误消息。我错过了什么?
答案 0 :(得分:2)
您没有看到错误消息,因为您不需要检查它们。您错误地调用了mysqli_query
,并且由于您没有检查错误,因此永远不会看到它们:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
^^^^---required
由于您使用不正确,查询调用返回false。然后,您盲目地尝试从该布尔值FALSE获取结果行,这将导致进一步的错误,并且您的while()
循环根本不会执行。