Jamie Obrien Jamie Obrien - 1 year ago 83
PHP Question

while loop for retrieving data

I am trying to retrieve multiple cars from a car rental database based on the model, so if someone clicks on Ford it would retrieve all cars that have a Model ID of 2 for example. The current code I have only shows the first record in the database, how do I make a while loop that would echo the rows for each match?

$ModelID = $_GET['model_id'];

$result = mysqli_query($con, "SELECT RegNumber, Colour FROM Car WHERE ModelID = '$ModelID'");

$row = $result->fetch_assoc();

echo $row["RegNumber"];
echo $row["Colour"];

Answer Source

You need something like:

while ($row = $result->fetch_assoc()) {
    echo $row['RegNumber'];
    echo $row['Colour'];

For more details, please go through the PHP Documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download