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
https://forums.phpfreaks.com/topic/116063-accessing-two-tables-in-a-while-loop/
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 />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.