Reply to Re: retrieveing vars from mysql join ?

Your name:

Reply:


Posted by Erwin Moller on 12/01/06 13:20

= 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация