Association of multiple tables foreign key?

d5288dd291d7432695b1cef91729a71d.jpg
Good day, found a similar question on the forum but still could not understand. in the picture there are three tables, the first is connected with the second foreign key id in the second table, the fact that required when the request id is to substitute the value corresponding to this id from another table when the key is everything works but when I try two it does not work, point in the right direction please.
August 19th 19 at 22:58
1 answer
August 19th 19 at 23:00
Solution
SELECT `passengers`.`name`, `from`.`name`, `to`.`name` FROM `passengers`
 JOIN `cities` AS `from` ON `passengers`.`from` = `from`.`id`
 JOIN `cities` AS `to` ON `passengers`.`to` = `to`.`id`
thank you very much for the reply, I can't join your table names and your request. table 1 is called `orders` and have field o_id, cl_name, start_from, go_to, the second `loc` has a field loc_id, loc_name. I understand `passengers` = `orders` and `cities` = `loc`? - Reginald.Carter commented on August 19th 19 at 23:03
SELECT `orders`.`cl_name`, `from`.`loc_name`, `to`.`loc_name` FROM `orders`
 JOIN `loc` AS `from` ON `orders`.`start_from` = `from`.`loc_id`
 JOIN `loc` AS `to` ON `orders`.`go_to` = `to`.`loc_id`
- Zachary.Bednar98 commented on August 19th 19 at 23:06
: trying to get next cycle
$query_to_db = mysql_query("SELECT `cl_name`, `from`.`loc_name`, `to`.`loc_name` FROM `orders`
 JOIN `loc` AS `from` ON `start_from` = `from`.`loc_id`
 JOIN `loc` AS `to` ON `go_to` = `to`.`loc_id`;");

while($r=mysql_fetch_array($query_to_db))
{

print_r($r);

echo $r['0'].",";
echo $r['1'].",";
echo $r['2']."<br>";

}


get here the answer:
Array ( [0] => Bob [cl_name] => Bob [1] => Berlin [loc_name] => Paris [2] => Paris ) Bob,Berlin,Paris
Array ( [0] => Peter [cl_name] => Peter [1] => Warsaw [loc_name] => Paris [2] => Paris ) Peter,Warsaw,Paris
everything works thanks to you but I don't understand why the array has such appearance? usually the key=>value, and here it turns out the key=>value,column name=>value and key [2] normal mapping - Reginald.Carter commented on August 19th 19 at 23:09
you need to set a flag to fetch_array. Without the flag, it returns an array with numeric and string keys. - Zachary.Bednar98 commented on August 19th 19 at 23:12
: found the answer here https://books.google.com.ua/books?id=SF3nCQAAQBAJ&... it turns out need to be redone as in the example or my code can be changed to set a flag? - Reginald.Carter commented on August 19th 19 at 23:15
under this option,$r=mysql_fetch_array($query_to_db,MYSQL_ASSOC)the answer is not complete and returns
Array ( [cl_name] => Bob [loc_name] => Paris ) 
Array ( [cl_name] => Peter [loc_name] => Paris )
- Reginald.Carter commented on August 19th 19 at 23:18
Are unable to test TK php7, but should work.
Look toward the PDO. The mysql extension is deprecated and removed in recent versions in favor of more advanced.
And about the book - like the heavy legacy of the sixth version of PHP. Don't read it. It has long been obsolete. - Zachary.Bednar98 commented on August 19th 19 at 23:21
: thank you very much that answered the questions, without you even would have a long time before the truth would go) - Reginald.Carter commented on August 19th 19 at 23:24
Yes nesest - Zachary.Bednar98 commented on August 19th 19 at 23:27

Find more questions by tags MySQL