Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble joining three MySQL tables

Status
Not open for further replies.

rskuse

Technical User
Joined
Jul 18, 2002
Messages
74
Location
GB
I am using Dreamweaver MX and I am having trouble linking three MySQL tables. My code is as follows:

if (isset($HTTP_GET_VARS['subcat_id'])) {
$colname_SubCategory = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['subcat_id'] : addslashes($HTTP_GET_VARS['subcat_id']);
}
mysql_select_db($database_iserver, $iserver);
$query_SubCategory = sprintf("SELECT * FROM category, subcats, topics WHERE category.cat_id = subcats.cat_id AND subcats.subcat_id = topics.subcat_id AND %s = subcat_id", $colname_SubCategory);

The trouble i'm having is that the same subcat is being returned for each subcat_id (where subcat is a field in my subcats table).

That is to say that...
../subcat.php?subcat_id=1
../subcat.php?subcat_id=2
../subcat.php?subcat_id=3
are all returning the same subcat from the database.

Everything else seems to be working fine. Can anyone tell me where I'm going wrong?

Thankyou in advance...

Rachel


 
It also looks as if MySQL should complain about the statement as there is an ambiguos identifier subcat_id - it could be in topics or subcats.

Do you have error checking on the mysql_query()?
 
Sorry, I did have an ambiguous identifier. It is actually subcats.subcat_id.

How do I specify the actual row to return?
 
Just for clarity:
Wouldn't you compare the database column to the entered value rather than vice versa? I think that's what sleipnir is pointing out.
Code:
%s = subcats.subcat_id # the passed value is first
subcats.subcat_id = %s # that looks better
 
I get the same result....

I really do appreciate your help, I'm still learning php, MySQL and 'joining tables' is confusing me just a tad.
 
Echo out the SQL statement and paste it into your favorite MySQL admin tool (PHPMyAdmin etc.)
What is the result there?

Also:
Write the pseudo-op code for the SQL. That will help clarify what it is you want to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top