0
<?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 |
+----+---------+----------+
1
  • Using while. It's written in all manuals. Commented May 28, 2017 at 16:23

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.