Amalia
Amalia

Reputation: 33

How can an SQL query return data from multiple rows

<?php
$con = mysqli_connect("localhost","root","","register");
$sql = "SELECT * FROM photos WHERE user_id ='$id'";
$result = mysqli_query($con,$sql);
$array = mysqli_fetch_assoc($result);
$rowcount = mysqli_num_rows($result);
$source  = $array['photo'];
?>

I want to get data from colomn photo, but if I have several rows with the same user_id, $source = $array['photo'] returns me data only from first row. How can i get data from all rows with same user_id??

and my DB is:

+----+---------+----------+
| id | user_id | photo    |
+----+---------+----------+
|  1 |    14   | 1010.jpg |
|  2 |    14   | 1013.jpg |
|  3 |    14   | 1210.jpg |
|  4 |    10   | 1173.jpg |
|  5 |    20   | 2038.jpg |
+----+---------+----------+

Upvotes: 0

Views: 45

Answers (1)

PeterMader
PeterMader

Reputation: 7285

<?php
$con = mysqli_connect("localhost","root","","register");
$sql = "SELECT * FROM photos WHERE user_id ='$id'";
$result = mysqli_query($con,$sql);
$rowcount = mysqli_num_rows($result);

while ($row = mysqli_fetch_assoc($result)) {
  $source = $row['photo];
  // do stuff with $source
}
?>

Take a look at the docs about mysqli_fetch_assoc.

Upvotes: 1

Related Questions