Jump to content

Problem with SQL select into multiple table


patheticsam

Recommended Posts

Hi, I'm a little bit new to php and i'm having a small problem. Let me explain with the script :

 

<?php

//connect to mysql

$data = mysql_query("SELECT * FROM forum_topic WHERE topic_id='$topic'") 
or die(mysql_error());

while($info = mysql_fetch_array( $data ))

$dete = mysql_query("SELECT * FROM forum_topic WHERE login='$info['username']'") 
or die(mysql_error());

while($info2 = mysql_fetch_array( $dete ))

     {

echo "......";
     }
?>

 

So I have the first SELECT command that will get the data from table forum_topic and array the data and after that I have a second SELECT that will get the data from table_members but this time time the WHERE clause equals to some data "arrayed" in the first SELECT command...And the end I need to output the data from both tables....

 

Here's the error I get :

 

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/m/a/z/mazemontana/html/login/forum_viewtopic.php on line 73

 

 

...I know that my syntax is probably not good but can't figure out how to do it.....

 

Any help will be greatly appreciated!

 

Thanx

<?php

//connect to mysql

$data = mysql_query("SELECT * FROM forum_topic WHERE topic_id='$topic'") 
or die(mysql_error());

while($info = mysql_fetch_array( $data ))
[b]{[/b]
$dete = mysql_query("SELECT * FROM forum_topic WHERE login='$info['username']'") 
or die(mysql_error());

while($info2 = mysql_fetch_array( $dete ))

     {

echo "......";
     }

[b]}[/b]
?>

 

Associative array elements are a little different when used in that context. You'll need to either concatenate them into the string:

WHERE login='" . $info['username'] . "'")

 

Or you can enclose them in curly braces:

WHERE login='{$info['username']}'")

 

Personally, I find the curlies to be more readable, but either is equally valid.

Then you have a query failing, or returning an empty result set, most likely. Have you echoed your variables to make sure they're populated? I don't see where $topic is ever assigned a value. Also, executing a query in a loop like that is a resource hog from hell.

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.