我在数据库中有 3 顿饭。当我显示我的查询时,它显示 3 个价格。我如何只显示一个结果?
$result = mysqli_query($con, "SELECT meal, price FROM Meals");
while ($row = $result->fetch_assoc()) {
echo '<input name="price" type="text" value="'.$row['price'].'" />';
}
正确的查询是
$result = mysqli_query($con, "SELECT meal, price FROM Meals GROUP BY meal HAVING meal = "mealname" ");
如果你想显示任何一个出三个然后使用
$result = mysqli_query($con, "SELECT meal, price FROM Meals LIMIT 1");
if ($row = $result->fetch_assoc()) {
echo '<input name="price" type="text" value="'.$row['price'].'" />';
}
如果您想显示特定的,则在查询中使用 id 或任何主键。喜欢
$result = mysqli_query($con, "SELECT meal, price FROM Meals WHERE id='your id or primary key value' LIMIT 1");
if ($row = $result->fetch_assoc()) {
echo '<input name="price" type="text" value="'.$row['price'].'" />';
}
简而言之,根据您想要的结果使用您的查询。
使用此查询显示一顿饭的价格。
$result = mysqli_query($con, "SELECT meal, price FROM Meals WHERE meal = "mealname" ");
while ($row = $result->fetch_assoc()) {
echo '<input name="price" type="text" value="'.$row['price'].'" />';
}