Jump to content

Accessing two tables in a while loop?


mrhorror

Recommended Posts

Hi

 

I wan't to create a code that in a while loop list the content of table1 while looking for a matching result in table2.

 

Table1 contains the main data

Table2 contains keywords and description for the names in table1.

 

example:

Table1

ID - Title

1 - Intel core 2

2 - Intel Quad

3 - Amd 3200+

 

Table2

Titl - Description

Intel - A Processor

Amd - Another Processor

 

 

This code below kinda shows what i wan't todo, but does obviously not work  :)

//list from table1
while(list($id, $title) = mysql_fetch_row($go->lists)) {
$i++;
echo $title;
//list from table2
$query = "SELECT * FROM info where titl LIKE '%$title%'";
$result = mysql_query($query);
if (mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
echo $row['Description'];
}else{
}
}

 

Hope you understand what i mean, i'm not so good at english  :)

 

Thanks

Link to comment
Share on other sites

You'd use a join.

 

Could you please give me a small sample code?  :)

 

Thanks

 

i have tried this but that doesn't work

 

$query = "SELECT table1.title, table2.titl ".
"FROM table1, table2".
"WHERE table2.titl = table1.title";

$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
echo $row['title']. " - ". $row['Description'];
echo "<br />";
}

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.