Jump to content

[php and mysql] query question.


newrehmi

Recommended Posts

let say I have 2 table, which is user and photo table

 

user may have many photo, while a photo only have 1 user.

so I made user_id (foreign key)column in my photo table..

 

lemme give an example about my photo table..

 

  id   | name      |   url       |   user_id    |
  1    | bus       | bus.jpg     |     1        |
  2    | car       | car.jpg     |     1        |
  3    | plane     | plane.jpg   |     1        |
  4    | train     | train.jpg   |     2        |
  5    | bicycle   | bcycle.jpg  |     2        |

 

so, I now abit confusing about what query to run. My question is, how to echo 'all' the value on name and url, based only on user_id=1. May someone tell me, what query to run, and what function do I need to use?

 

If using 'while' how?

 

btw, I am making an image gallery...

Link to comment
Share on other sites

Perhaps something like this?

 

$query = mysql_query("SELECT * FROM photos WHERE user_id = '1'");
while($row = mysql_fetch_array($query)) 
{
    echo $row["id"] . ' - ' . $row["name"] . ' - ' . $row["url"] . '<br />';
}

 

This should output:

 

1 - bus - bus.jpg
2 - car - car.jpg
3 - plane - plane.jpg

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.