Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
One Query, Selecting Muliple Tables...
Old 07-11-2011, 02:30 AM One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
For the life of me, I cannot get this query right.

PHP Code:
$query "SELECT category_name, subcategory_id FROM categories LEFT JOIN subcategories ON subcategories.subcategory_id = categories.category_name ORDER BY category_name, subcategory_id"
The url:
PHP Code:
<?php echo '<a href=categories.php?id='.$row['subcategory_id'].'>'.$category.' </a>';?>
The above query does display on the page without errors, but the objective here is to have a link with category_name (from the categories table) as the text on the page, and subcategory_id (from the subcategories table) as the link itself.

The output url is: http://reunitemysite.com/categories.php?id=

There is no id in the url. I want the url to link to the subcategory id in the subcategories table.

Using Php 5.2.17
MySQL 5.1.52

Boy, I wish they'd standardise php and mysql code once and for all, I am tired of trying to find the solution when all it is, is a minor syntax issue that's always next to impossible to find. What's the deal with that? I've tried many solutions, but none worked for me. I am sure it's probably something very simple too.
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-11-2011 at 02:36 AM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-11-2011, 05:43 AM Re: One Query, Selecting Muliple Tables...
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
And the code that creates $row is????
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-11-2011, 12:22 PM Re: One Query, Selecting Muliple Tables...
King Spam Talker

Posts: 1,110
Name: Paul W
Trades: 0
You've either got a wrong query or a badly named field. Can you show the two table definitions.
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 07-11-2011, 06:22 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by chrishirst View Post
And the code that creates $row is????
I would have thought the code was already complete...Care to explain what you mean by that?
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-11-2011, 06:27 PM Re: One Query, Selecting Muliple Tables...
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Nope, you have posted the query concatenation and the output getting written to the user agent, but the bit where the query is sent to the SQL server and a recordset is returned is missing.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-12-2011, 06:30 AM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by chrishirst View Post
Nope, you have posted the query concatenation and the output getting written to the user agent, but the bit where the query is sent to the SQL server and a recordset is returned is missing.

Oh you mean this? I left it out of the original post, sorry.

// THIS PART
PHP Code:
$result mysql_query($query) or die(mysql_error()); 
I had it there, it still didn't work as expected. Which was because the row $row[subcategory_id] was blank in the url. I double even triple checked the row name was spelled correctly and it exists.

Still no dice.
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-12-2011 at 06:39 AM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 01:32 PM Re: One Query, Selecting Muliple Tables...
Average Talker

Posts: 20
Trades: 0
In the SQL query you have:
PHP Code:
LEFT JOIN subcategories ON subcategories.subcategory_id categories.category_name 
I guess that categories.category_name is a VARCHAR (i mean, the name of this cat) and subcategories.subcategory_id is a INT. You should "connect" subcategory_id with CATEGORY_ID, not with CATEGORY_NAME
hellboy124 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 01:57 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by hellboy124 View Post
In the SQL query you have:
PHP Code:
LEFT JOIN subcategories ON subcategories.subcategory_id categories.category_name 
I guess that categories.category_name is a VARCHAR (i mean, the name of this cat) and subcategories.subcategory_id is a INT. You should "connect" subcategory_id with CATEGORY_ID, not with CATEGORY_NAME
After "connecting" subcategory_id with CATEGORY_ID, I am getting this error:

Code:
Column 'category_id' in field list is ambiguous
Here's what I changed to:

PHP Code:
$query "SELECT category_id, subcategory_id FROM categories LEFT JOIN subcategories ON subcategories.subcategory_id = categories.category_id ORDER BY category_id, subcategory_id"
Thanks again!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 02:51 PM Re: One Query, Selecting Muliple Tables...
Average Talker

Posts: 20
Trades: 0
Ups i didn't realized... do you have "cat_id" in your subcat table?

How does your tables look like?

You should do it like that...

CATEGORIES
- category_id
- category_name
- ...

SUBCATEGORIES
- subcategory_id
- subcategory_cat_id
- subcategory_name
- ...

then would be the query like that:
PHP Code:
$query "SELECT categories.category_id, subcategories.subcategory_cat_id FROM categories LEFT JOIN subcategories ON subcategories.subcategory_cat_id = categories.category_id ORDER BY categories.category_id, subcategories.subcategory_cat_id"
hellboy124 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 03:04 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Ok,

I am going to check that right now.

Finally, got the mysql query right. Man I thought I'd never get that. One more issue though...How come the link is not making a new page? I am looking for the link to open up to a page (dynamically). The links ARE correct though that they show the correct id.

The page:
http://reunitemysite.com/categories.php

Thank you for your help Hellboy.

-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-12-2011 at 03:52 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 04:37 PM Re: One Query, Selecting Muliple Tables...
Average Talker

Posts: 20
Trades: 0
PHP Code:
if ( isset($_GET['id']) && is_numeric($_GET['id'] ) {

   
// Output that, what you want, when a ID is clicked

} else {

    
// Output categories table


hellboy124 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 10:03 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by hellboy124 View Post
PHP Code:
if ( isset($_GET['id']) && is_numeric($_GET['id'] ) {

   
// Output that, what you want, when a ID is clicked

} else {

    
// Output categories table


Sorry for not understanding the above, Could you put some sample in the comment sections?

I was thinking on these lines, (It was from another script online) but it didn't work all it gave me was this is where the error occurs message:

PHP Code:
//Check for a valid user ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) 


    

//From view_users.php

    

$id $_GET['id'];

elseif( (isset(
$_POST['id'])) && (is_numeric($_POST['id'])) ) 


    

// Form submission

    

$id $_POST['id'];

else 


    

// No valid ID . kill the script.

    

echo '<p class="error">This is where the error occurs.</p>';

    

//include('footer.html');

    

//exit();

Thank you
Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-12-2011 at 10:06 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 11:02 PM Re: One Query, Selecting Muliple Tables...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by Brian07002 View Post
I am getting this error:

Code:
Column 'category_id' in field list is ambiguous
FYI -The "ambiguous" error means that the column name exists in more than one called table. You would have to associate the table for each column referred to such as table_name.column_name or alias.column_name
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-12-2011, 11:57 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by mgraphic View Post
FYI -The "ambiguous" error means that the column name exists in more than one called table. You would have to associate the table for each column referred to such as table_name.column_name or alias.column_name
That's correct.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-13-2011, 04:45 AM Re: One Query, Selecting Muliple Tables...
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Brian, the extra bit of code you posted has $result as the recordset variable, but your original code has $row as the recordset variable.
One of them must be wrong or there is your "problem"
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-13-2011, 06:42 AM Re: One Query, Selecting Muliple Tables...
Average Talker

Posts: 20
Trades: 0
I will give you a example, what i ment.

categories.php
PHP Code:
<?php

// create conection to DB
$db_link mysql_connect("db_host""db_user""db_pass");
$db_name mysql_select_db("db_name");

if ( isset(
$_GET['id']) && is_numeric($_GET['id'] ) {

    
$cat_id $_GET['id']

    
$subcats_query_sql "SELECT * FROM categories, subcategories WHERE categories.category_id = $cat_id AND categories.category_id = subcategories.subcategory_cat_id";

    
$subcats_query mysql_query($subcats_query_sql);

   echo 
"<ul>";
    while ( 
$row mysql_fetch_array($subcats_query) ) echo "<li>$row['subcategory_name']";
    echo 
"</ul>";

    
// Outputs you a list of subcategories in this category

} else {

    
$cat_query_sql "SELECT * FROM categories";

    
$cat_query mysql_query($cat_query_sql);

    echo 
"<ul>";
    while ( 
$row mysql_fetch_array($cat_query) ) echo "<li><a href=categories.php?id=$row['category_id']>$row['category_name']</a>";
    echo 
"</ul>";

    
// This is the categories list



?>
hellboy124 is offline
Reply With Quote
View Public Profile
 
Old 07-13-2011, 06:45 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by hellboy124 View Post
I will give you a example, what i ment.

categories.php
PHP Code:
<?php

// create conection to DB
$db_link mysql_connect("db_host""db_user""db_pass");
$db_name mysql_select_db("db_name");

if ( isset(
$_GET['id']) && is_numeric($_GET['id'] ) {

    
$cat_id $_GET['id']

    
$subcats_query_sql "SELECT * FROM categories, subcategories WHERE categories.category_id = $cat_id AND categories.category_id = subcategories.subcategory_cat_id";

    
$subcats_query mysql_query($subcats_query_sql);

   echo 
"<ul>";
    while ( 
$row mysql_fetch_array($subcats_query) ) echo "<li>$row['subcategory_name']";
    echo 
"</ul>";

    
// Outputs you a list of subcategories in this category

} else {

    
$cat_query_sql "SELECT * FROM categories";

    
$cat_query mysql_query($cat_query_sql);

    echo 
"<ul>";
    while ( 
$row mysql_fetch_array($cat_query) ) echo "<li><a href=categories.php?id=$row['category_id']>$row['category_name']</a>";
    echo 
"</ul>";

    
// This is the categories list



?>
I am getting an error on this line:
PHP Code:
if ( isset($_GET['id']) && is_numeric($_GET['id'] ) { 
Parse error: syntax error, unexpected '{ in categories.php

Ps: I know it may not be relating, but I changed the db info, so that shouldn't be the problem.
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-13-2011 at 07:21 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-13-2011, 11:05 PM Re: One Query, Selecting Muliple Tables...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
You are missing the closing ) on your if statement
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-14-2011, 05:56 PM Re: One Query, Selecting Muliple Tables...
Brian07002's Avatar
Defies a Status

Posts: 2,312
Name: ...
Location: ...
Trades: 1
Quote:
Originally Posted by mgraphic View Post
You are missing the closing ) on your if statement
After adding that closing parenthesis ) to this line:
PHP Code:
if ( isset($_GET['id']) && is_numeric($_GET['id'] ) ) { 
I am getting this error:
Code:
Parse error: syntax error, unexpected T_VARIABLE in categories.php on this line:
PHP Code:
$subcats_query_sql "SELECT * FROM categories, subcategories WHERE categories.category_id = $cat_id AND categories.category_id = subcategories.subcategory_cat_id"
Any ideas?

Thank you in advance.
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-14-2011, 06:04 PM Re: One Query, Selecting Muliple Tables...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
You are missing a ; on the line before it
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to One Query, Selecting Muliple Tables...

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51456 seconds with 11 queries