473,503 Members | 9,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retrieveing vars from mysql join ?

Hi all ,

I have the following problem :

$some_query = mysql_query(
"SELECT table1.id, table1.category, table1.author, table1.title,
table2.title, table2.category FROM table1, table2
WHERE table1.category = table2.category ORDER by table1.id DESC");

while($record = mysql_fetch_array($some_query))
{
$mycategory = ($record['table1.category']);
$mytitle = ($record['table2.title']);

echo"$mycategory";
echo"$mytitle";
}

Does someone know what's wrong here ?
echo don't show the requested values ....
thanks !
Dec 1 '06 #1
5 1216
= poster = wrote:
Hi all ,

I have the following problem :

$some_query = mysql_query(
"SELECT table1.id, table1.category, table1.author, table1.title,
table2.title, table2.category FROM table1, table2
WHERE table1.category = table2.category ORDER by table1.id DESC");

while($record = mysql_fetch_array($some_query))
{
$mycategory = ($record['table1.category']);
$mytitle = ($record['table2.title']);

echo"$mycategory";
echo"$mytitle";
}

Does someone know what's wrong here ?
echo don't show the requested values ....
thanks !
Hi,

Many databases return their results without table specification.
However, If you have 2 columns with the same name you MUST specify which one
you need in the query, or the database will start complaining about it.

The way around this problem is simple, use an alias in the query, and do not
give the tablenames in PHP.

In your case:
$some_query = mysql_query(
"SELECT table1.id,
table1.category AS cat1 ,
table1.author,
table1.title as title1,
table2.title as title2,
table2.category AS cat2
FROM table1, table2
WHERE table1.category = table2.category ORDER by table1.id DESC");

while($record = mysql_fetch_array($some_query))
{
$mycategory = ($record['cat2']);
$mytitle = ($record['title2']);

echo"$mycategory";
echo"$mytitle";
}

(Not tested)

Good luck and regards,
Erwin Moller
Dec 1 '06 #2
= poster = wrote:
Hi all ,

I have the following problem :

$some_query = mysql_query(
"SELECT table1.id, table1.category, table1.author, table1.title,
table2.title, table2.category FROM table1, table2
WHERE table1.category = table2.category ORDER by table1.id DESC");

while($record = mysql_fetch_array($some_query))
{
$mycategory = ($record['table1.category']);
$mytitle = ($record['table2.title']);

echo"$mycategory";
echo"$mytitle";
}

Does someone know what's wrong here ?
echo don't show the requested values ....
thanks !

A stupid question - but are you actually calling mysql_query() before
trying the fetch the results?

Then try the following - and look at your index values:

echo "<pre>\n";
print_r($record);
echo "</pre>\n";

And finally, you can make things easier on yourself with something like:

"SELECT ... table1.category AS tab1category,
... table2.category AS tab2category ...

Also, if a column has a unique name in the tables listed in your FROM
clause, just use the column name. You only need the table name when you
have duplicate column names, as in category.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 1 '06 #3
Thanks a lot for your help !

The problem was that I dind't know the 'AS' alias function.

Does the aliasing have an impact on the speed of the query ?

thanks a lot !
Dec 1 '06 #4
= poster = wrote:
Thanks a lot for your help !

The problem was that I dind't know the 'AS' alias function.

Does the aliasing have an impact on the speed of the query ?
Probably a weee little, but I wouldn't care about that effect too much.

Regards,
Erwin
>
thanks a lot !
Dec 1 '06 #5
= poster = wrote:
Thanks a lot for your help !

The problem was that I dind't know the 'AS' alias function.

Does the aliasing have an impact on the speed of the query ?

thanks a lot !

Nothing measurable, at any rate. There might be if you had a few
hundred aliases, but for a few I've never been able to measure a
difference the margin of error in the system.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2013
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
0
3925
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
33
2989
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
6
2602
by: goodguyjam | last post by:
Hi all, I'm having trouble with mysql. I've just finished my php coding for HTTP authentication and with some help am now getting a login window pop up whenever I click on a link on my website...
1
1736
by: svsenthilkumar | last post by:
how can i add 10 days with retrieveing date using php i retrieve a date from mysql table,and i add 10 days with that date and compare with today is or not.
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.