简体   繁体   中英

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 |
+----+---------+----------+
<?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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM