View Full Version : Getting from Database

03-03-2010, 10:47 PM
In one table of my database called 'users' I store values called `id`, `giftID`, `gifttitle` and `value`.

In another called 'gifts' I store `id`, `gifttitle`, `image` and `value`.

Now, I need to be able to call the id from users to grab the gift title, image and value that the user has selected.

How would I be able to do this?

I tried using this code:

$ugid = $_SESSION['id'];
$giftresult = mysql_query("SELECT * FROM users WHERE id='$ugid'");
$giftusers = mysql_fetch_array($giftresult);
$giftid = $giftusers['giftID'];

$result = mysql_query("SELECT * FROM gifts WHERE id='$giftid'");
while($list = mysql_fetch_array( $result )){
echo $list['giftID'];
However that just lists all the gift id in the users field stored in the database.


03-03-2010, 11:09 PM
If I understood correctly:

$result = mysql_query("SELECT * FROM users, gifts WHERE users.id = '$ugid' && gifts.id = users.giftID");
$list = mysql_fetch_array( $result );
echo $list['gifttitle'];

That code should give you the giftTitle that corresponds to the giftID stored in your `users` table...

And likewise $list['image'] should give you the image etc etc


03-04-2010, 05:57 AM
Ya, i think thats right, you just didnt change the echo to the correct column