0

I have 2 tables affilie and avantage and every one has an attribute named id, I made a query using left join. how I can distinguish those tow id having the same name when I want to use them.

$query="SELECT * FROM affilie af left join avantage av on af.id = av.id WHERE id='".$_GET['id']."' ";
$req = mysql_query($query) or die(mysql_error());
$data=mysql_fetch_assoc($req);
hous
  • 174
  • 4
  • 11
  • 2
    You use the table prefixes `av` and `af`: `av.id` and `af.id` – scrowler May 08 '14 at 00:28
  • You have SQL injection, [read this](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and [**Don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Lawrence Cherone May 08 '14 at 00:28
  • @LozCherone ... and yet another irrelevant comment in a misled crusade. As usual you didn't read the question. – Eugen Rieck May 08 '14 at 00:30
  • 1
    @scrowler, @HamZa: The table prefixes show up in the JOIN clause, but not in `mysql_fetch_assoc()` – Eugen Rieck May 08 '14 at 00:31
  • 1
    @LozCherone Can you stop with the don’t use `mysql_*` nonsense. You have 100% no idea where this code will be residing or how the poster will be using it. A lot more servers are running PHP 5.2 & older than you think. Wince as you might, but that’s the reality of OS upgrades. Address the question which in this case is quite valid. – Giacomo1968 May 08 '14 at 00:37
  • 1
    @LozCherone You are correct that SQL injection is a risk. How do you know this PHP app will be publicly accessible? How exactly did you “better” his code by solving a problem the original poster does not have issues with? How do you know the rest of the code is bullet proof? Also, as a Unix systems admin with 20+ years experience, I have seen PHP 5.3 & 5.4 installs hacked without SQL injection. There are risks in everything. Period. The obsession folks have chastising the use of `mysql_*` code really comes off like someone lecturing someone on eating their vegetables. Preachy & pedantic. – Giacomo1968 May 08 '14 at 00:56

1 Answers1

5

You need to alias them on the SQL level:

SELECT
  af.id AS afid,
  af.somefield AS somefield,
  -- ...
  av.id AS avid,
  av.otherfield AS otherfield
  -- ,...
FROM
  ...
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92