Solved

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

Posted on 2011-09-30
4
318 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
ID: 36893178
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
ID: 36893186
have you tried defining $id as $row["bryggplatser.id"]  ??
0
 
LVL 41

Expert Comment

by:ralmada
ID: 36893189
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
ID: 36894816
Excellent! Thanks!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

19 Experts available now in Live!

Get 1:1 Help Now