mehdi Mollazehi
mehdi Mollazehi

Reputation: 35

How to fetch several rows from mysql db with php

I have a table in a database that has multiple fields with an ID. I want to fetch all rows with ID=1 (for example ID=1 has 3 rows in the table). I have written a query to fetch the rows with ID=1 from the db, but this query only fetches the first row. I want to fetch all rows of the ID=1.

How i can fetch all rows of ID=1?

My PHP Code:

 $conn = mysqli_connect('localhost', 'root', '', 'win');
 $query = "SELECT * FROM lights WHERE id='1'";
 $result = mysqli_query($conn, $query);
 $row = mysqli_fetch_assoc($result);
 print_r($row);
?>```

Upvotes: 0

Views: 70

Answers (1)

Haley Mueller
Haley Mueller

Reputation: 497

You need to loop through mysqli_fetch_assoc($result)

while ($row = mysqli_fetch_assoc($result)) { //Goes through each row of the query
    print_r($row); //A row from the table
}

Source: mysqli_result

Upvotes: 2

Related Questions