Accessing two tables in a while loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrhorror
Forum Newbie
Posts: 2
Joined: Tue Jul 22, 2008 2:03 pm

Accessing two tables in a while loop?

Post by mrhorror »

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 :)

Code: Select all

 
//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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Accessing two tables in a while loop?

Post by WebbieDave »

mrhorror wrote:but does obviously not work
How so? What happens when the code is run? Do you receive an error message from the database? Or just an empty result set? Try changing line 8 to:

Code: Select all

$result = mysql_query($query) or die(mysql_error());
to see if that sheds more light.
mrhorror
Forum Newbie
Posts: 2
Joined: Tue Jul 22, 2008 2:03 pm

Re: Accessing two tables in a while loop?

Post by mrhorror »

WebbieDave wrote:
mrhorror wrote:but does obviously not work
How so? What happens when the code is run? Do you receive an error message from the database? Or just an empty result set? Try changing line 8 to:

Code: Select all

$result = mysql_query($query) or die(mysql_error());
to see if that sheds more light.
Hi.

No errors at all.

Code: Select all

$result = mysql_query($query) or die(mysql_error());
Does die, but no error.


I tried creating another way todo the task with the code below, i get no errors but it does die
Anyone knows whats wrong? it should work :(

Code: Select all

while(list($id, $type, $title) = mysql_fetch_row($go->lists)) {
$i++;
$tiar['id'.$i] = $id;
$tiar['title'.$i] = $title;
}
 
//Description part, Does not work(die)!
 
$i = 1;
while($i <= 40){
$title = $tiar['title'.$i];
$result = mysql_query("SELECT * FROM info WHERE titl LIKE '%$title%'");
$row = mysql_fetch_assoc($result);
$tiar['Description'.$i] = $row['Description'];
$i++;
}
//end description part
 
$i = 0;
while($i <= 40){
echo $tiar['title'.$i]."<br>";
echo $tiar['Description'.$i];
$i++;
}
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Accessing two tables in a while loop?

Post by WebbieDave »

mrhorror wrote:Does die, but no error
If the die function was called, the error would print.
mrhorror wrote:i get no errors but it does die
Here you use the term die even though the die function isn't in your second code iteration. I'll assume that, by die, you mean the script does not perform as expected. I'm also assuming you receive no output (only a blank page) as you have not indicated otherwise. PHP will print a blank page on error in many configurations. If that's what's occurring you can override that behavior by placing this at the top of your code:

Code: Select all

ini_set('display_errors', 1);
error_reporting(E_ALL);
You'll also want to verify that your queries are returning records by employing some well-placed echo's.
Post Reply