4 Replies - 785 Views - Last Post: 05 February 2012 - 04:19 AM

Topic Sponsor:

#1 dameon51  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 73
  • Joined: 07-June 10

include duplicate fields on php query

Posted 02 February 2012 - 11:33 PM

Hey everyone.

I'm doing a join with 3 tables, they all have a field called 'id'.

When I do a query straight to mysql it returns the id fields like

id, id1, id2

This is what I want.

I run the same query in php, and it only get

id

Which has the value of the id2, so its just taking the last id field.

Anyway to make it include the duplicate ones?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: include duplicate fields on php query

#2 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: include duplicate fields on php query

Posted 02 February 2012 - 11:47 PM

please put up your query and let me see and i will try to help.
Was This Post Helpful? 0
  • +
  • -

#3 dameon51  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 73
  • Joined: 07-June 10

Re: include duplicate fields on php query

Posted 02 February 2012 - 11:51 PM

SELECT products.*, product_gallery_map.*, image_gallery.* 
FROM products 
LEFT JOIN product_gallery_map ON product_gallery_map.productID=products.priKeyID 
LEFT JOIN image_gallery ON image_gallery.priKeyID=product_gallery_map.galleryID 
WHERE products.active= '1' ORDER BY products.ordinal

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1758
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: include duplicate fields on php query

Posted 03 February 2012 - 06:55 AM

You can use aliases to set different names for columns and tables. For example:
SELECT
	p.id AS product_id,
	gm.id AS gallery_map_id,
	ig.id AS gallery_id
FROM product_table AS p
LEFT JOIN product_gallery_map AS gm
	ON gm.productID = p.id
LEFT JOIN image_gallery AS ig
	ON gm.galleryID = ig.id


Was This Post Helpful? 1
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 361
  • View blog
  • Posts: 1,020
  • Joined: 30-January 09

Re: include duplicate fields on php query

Posted 05 February 2012 - 04:19 AM

Selecting '*' is something you should avoid for many reasons, and this attribute namespace clash is one of them. There are very few instances where selecting * is both necessary and safe.

Following Atli's advice is your best bet, as it both resolves your problem, and is a good programming technique.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1