How to retrieve sequential rows AND SINGLE ROW from a table?


In the below code:

 $sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

Is it possible to echo 1 row rather than each row sequentially (i.e. use the ‘get’ function to get a variable passed from the URL and match it to get the results including that variable in a single row of a MySQL table).

See below for kind of thing I am looking for:

$sql = “SELECT id, firstname, lastname FROM MyGuests”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $singlerow["repeat this value in every row where surname is in the same row as $get[id]"]. "<br>";

So if the ID was passed from the URL, how would I get the surname corresponding with that row in the MySQL table repeated for every row?

Thanks for your help,


It may be me, but I don’t quite understand what you’re trying to achieve.
Can you give a data example?

So if you have

ID: 1, 2, 3
Car: BMW, Mercedes, Ford
Mileage: 1000, 20000, 15000

I know how to use PHP to show the MySQL data with sequential rows.

I do not know how to have it like this:

id, make, mileage
1 BMW 20000
2 Mercedes 20000
3 Ford 20000

Obviously in real-life you would not want to have the mileage showing the mileage of the Mercedes for all cars.

So if the URL was

How do I code it to show 20000 mileage for all cars? I would need to create the table using the code I posted originally but I also need to access a single row and repeat it rather than sequential rows.

Understand what I am trying to achieve now?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.