在浏览器中多次显示价格[关闭]

I have 3 meals in the database. When I display my query it shows 3 prices. How do I only show one result instead?

$result = mysqli_query($con, "SELECT meal, price FROM Meals");

while  ($row = $result->fetch_assoc())  {
    echo '<input name="price" type="text" value="'.$row['price'].'" />';
}

You'll want to use the MySQL LIMIT clause.

$result = mysqli_query($con, "SELECT meal, price FROM Meals LIMIT 1");

From the MySQL manual:

With one argument, the value specifies the number of rows to return from the beginning of the result set.

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.

The proper query would be

$result = mysqli_query($con, "SELECT meal, price FROM Meals GROUP BY meal HAVING meal = "mealname" ");

if you want to show any one out three then use

$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'].'" />';
       }

if you want to show a specific one then use id or any primary key in your query. like

$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'].'" />';
       }

in short use your query according to what kind of result do you want.

Use this query to show one price for one meal. $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'].'" />'; }