I have at least 4 tables in MySQL for my PHP application (shortened it for this example)
Agents
– Agent_ID
– Agent_Name
Country
– Country_ID
– Country_Name
Job
– Job_ID
– Job_Type
Line_Items
– Line_ID
– Agent_ID
– Country_ID
– Job_ID
Now, I need to select from Line_Items where Agent_ID = 1, and instead of echo-ing the Agent_ID, Country_ID and Job_ID in their integers, I would like to output their names instead (Agent_Name, Country_Name, Job_Type).
- How do I write the query?
- How do I output this in PHP, using the well-used $result = mysql_query(“select ….”); while($row = mysql_fetch_query($result)){echo …..};
You need to join the tables:
Also, consider using a view, like so:
Then, using the view, your query is as simple as:
With any of these queries, you can the use the PHP code that you wrote to output the results.
Hope it helps.
EDIT
Using the first query, your PHP would be something like this (simplified):
Of course, you need to change the
LI.Agent_IDif you need a different ID. You can just use a placeholder for it and replace for the correct ID, or concatenate the correct ID to the query.