Jump to content

Displaying data from two tables


slack

Recommended Posts

Hi all,

I have problem with displaying data from two tables. There are two tables as follows:

TABLE1
ID-------NAME
1--------John
2--------Mark
3--------Kelly

TABLE2
ID-------REF------REFNAME
1--------A1--------Mark
2--------A2--------Kelly
3--------A3--------Kelly
4--------A4--------Mark

As you can see REFNAME in TABLE2 referances NAME in TABLE1.

In php I want the information to be displayed like this:

John
NONE

Mark
A1
A4

Kelly
A2
A3

I am not sure if this can be done by a SQL JOIN or some sort of loop.

Can someone help me display the information I want.

Thanks
Slack
Link to comment
Share on other sites

Problem solved.

Had to use 2 loops. Code looks something like this:

[code]
$query = "SELECT * FROM TABLE1";
$result = @mysql_query ($query);

while($row = @mysql_fetch_array($result))
{
echo $row["NAME"];
echo '<br>';

$Name = $row["NAME"];
$query2 = "SELECT * FROM TABLE2 WHERE TABLE2.REFNAME='$Name';
$result2 = @mysql_query ($query2);

while($row2 = @mysql_fetch_array($result2))
{
echo $row2["REFNAME"];
}
echo '<p> </p>';
}
[/code]


Slack
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.