Jump to content

[SOLVED] mysql / php nested query


shanman

Recommended Posts

Hi all,

 

I have 2 tables, 1 called favourites, the other merchant

 

For example:

 

favourites

id=user1 mercid=1

id=user1 mercid=3

id=user2 mercid=1

 

merchant

mercid=1 name=merc1 desc=blablabla

mercid=3 name=merc3 desc=blablabla

 

How can I write a php/mysql query to list the name and desc of each merchant according to the users login id? For example if user1 was logged in he would be presented with his favourite merchants and if it was user2 he would get his favourite merchants. The results will need to be output to a browser of course.

 

Hope this makes sense - I'm really struggling.

 

Thanks in advance

Link to comment
Share on other sites

Joins aren't my forte, I can't remember if it's MySQL that requires that same number of rows to link to, but here's a shot at it:

$query = "SELECT merchant.name,merchant.desc, favourites.id FROM merchant,favourites WHERE merchant.mercid=favourites.mercid";

Then loop through the results you want.

Here's a page on joins that can help you out, too: MySQL join tutorial.

Link to comment
Share on other sites

I gave it a try but it didnt work:

 

 

 

Here is the code I used (At the moment I just want to output the name of the Merchant - I'll work the detail when I get this bit working.)

 

$query = "SELECT merchant.name from merchant, favourites where favourites.mercid = merchant.id and favourites.user='$user')
$result = mysql_query($query)
while ($row = mysql_fetch_array($result)){
echo $row['name'];
}

 

Not sure if I explained things properly in the first post.

 

User A is logged in - he hits a link called favourites and the code should basically do the following:

 

select mercid from favourites where id='$user' (there will be several mercid's)

 

Then

 

For each mercid ...

 

Select name from merchants where id=mercid

 

and output the list of name to the browser.

 

Hope this makes sense cos this is driving me crazy lols.

Link to comment
Share on other sites

Managed to fix it myself but thanks for the help.

 

$sq = mysql_query("SELECT mercid from favourites where user='$user'");
$rows=mysql_num_rows($sq);
for($i=0; $i<$rows; $i++)
{
mysql_data_seek($sq,$i);
$arr=mysql_fetch_array($sq);
extract($arr);
$sq2 = mysql_query("SELECT * from merchant where id=$mercid");
$arr2=mysql_fetch_array($sq2);
extract($arr2);
echo"$name";
}

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.