0

How to get value in php from database when:

mysql_query("SELECT * FROM user 
INNER JOIN phone p1 ON user.IDPhone1 = p1.ID 
INNER JOIN phone p2 ON user.IDPhone2 = p2.ID )

columns of table phone: id, number, operator, model...

How to get for each user value "number" from table phone

Tried:

echo $r['p1.number']
echo $r['p2.number']

And

echo $r['user.IDPhone1.number']
echo $r['user.IDPhone2.number']

And many others :x

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • What does `var_dump($r)` returns? (; – joaumg Jan 08 '17 at 01:58
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 08 '17 at 01:58
  • just mistake when i write it here – krychu1324 Jan 08 '17 at 02:01
  • and when paste echo $r['number'] this return nothing – krychu1324 Jan 08 '17 at 02:02
  • 1
    If you have a column with the same name in 2 tables i.e. p1.number and p2.number you have to alias them to have a unique name in your query. So something like `SELECT p1.phone as p1phone, p2.phone as p2phone` otherwise you will only see one of them in the result set – RiggsFolly Jan 08 '17 at 02:03
  • When referencing them in the result set you dont get the table name used in the array. Do a `print_r($r)` to see what you have in the array – RiggsFolly Jan 08 '17 at 02:05

0 Answers0