Solved

Using join to get records from two database tables, how to distinguish "id" fields?

Posted on 2011-09-30
4
317 Views
Last Modified: 2013-11-19
Gurus,
I am using left join to get records from two database tables, both having "id" (same name in both tables) as Primary index.
This is the SQL-sentence:
$SQL = " SELECT * FROM bryggplatser left join matrikel on bryggplatser.medlemsnr = matrikel.MedlemsNr";

And then it goes on:

$ret = mysql_query($SQL);
if (!$ret) { echo( mysql_error()); }
else {
while ($row = mysql_fetch_array($ret)) {
$id = $row["id"]; Here I'd like to have "id" from the table "bryggplatser", but I get id from the table "matrikel".
$bryggplats = $row["bryggplats"]; This one correctly comes from the table "bryggplatser"
$medlemsnr = $row["medlemsnr"]; This one correctly comes from the table "bryggplatser"
$bredd = $row["bredd"]; This one correctly comes from the table "bryggplatser"
$djup_bryggan = $row["djup_bryggan"]; This one correctly comes from the table "bryggplatser"
$djup_bojsten = $row["djup_bojsten"]; This one correctly comes from the table "bryggplatser"
$bojtyngd_mtrl = $row["bojtyngd_mtrl"]; This one correctly comes from the table "bryggplatser"
$bojtyngd_vikt = $row["bojtyngd_vikt"]; This one correctly comes from the table "bryggplatser"
$katting_lina = $row["katting_lina"]; This one correctly comes from the table "bryggplatser"
$godsdiameter_botten = $row["godsdiameter_botten"]; This one correctly comes from the table "bryggplatser"
$godsdiameter_ytan = $row["godsdiameter_ytan"]; This one correctly comes from the table "bryggplatser"
$boj_typ = $row["boj_typ"]; This one correctly comes from the table "bryggplatser"
$boj_farg = $row["boj_farg"]; This one correctly comes from the table "bryggplatser"
$anmarkning = $row["anmarkning"];  This one correctly comes from the table "bryggplatser"      
$fornamn = $row["Fornamn"];  This one correctly comes from the table "matrikel"
$efternamn = $row["Efternamn"];   This one correctly comes from the table "matrikel"

What is the correct way to get id from the table "bryggplatser" without changing any name?
0
Comment
Question by:lericson
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
Comment Utility
declare the columns in your query explicitly like:


SELECT bryggplatser.id, bryggplatser.bryggplats, .... etc FROM bryggplatser left join matrikel on bryggplatser.medlemsnr = matrikel.MedlemsNr
0
 
LVL 6

Expert Comment

by:Dangle79
Comment Utility
have you tried defining $id as $row["bryggplatser.id"]  ??
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
And remember that it's never a good practice to use the "*"

because you not only encounter this kind of problem you're experiencing right now, but also you are slowing the query down by including unnecessary columns.
0
 

Author Closing Comment

by:lericson
Comment Utility
Excellent! Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now