Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 330633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:41:58+00:00 2026-05-12T09:41:58+00:00

I have at least 4 tables in MySQL for my PHP application (shortened it

  • 0

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).

  1. How do I write the query?
  2. How do I output this in PHP, using the well-used $result = mysql_query(“select ….”); while($row = mysql_fetch_query($result)){echo …..};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:41:58+00:00Added an answer on May 12, 2026 at 9:41 am

    You need to join the tables:

    SELECT A.Agent_Name, C.Country_Name, J.Job_Type
    FROM Line_Items LI, Agents A, Country C, Job J
    WHERE LI.Agent_ID = 1 AND LI.Agent_ID = A.Agent_ID AND
          LI.Country_ID = C.Country_ID AND LI.Job_ID = J.Job_ID
    

    Also, consider using a view, like so:

    CREATE VIEW Line_Items_Detail
    AS
      SELECT LI.Agent_ID, A.Agent_Name, C.Country_Name, J.Job_Type
      FROM Line_Items LI, Agents A, Country C, Job J
      WHERE LI.Agent_ID = 1 AND LI.Agent_ID = A.Agent_ID AND
            LI.Country_ID = C.Country_ID AND LI.Job_ID = J.Job_ID
    

    Then, using the view, your query is as simple as:

    SELECT Agent_Name, Country_Name, Job_Type
    FROM Line_Items_Detail
    WHERE Agent_ID = 1
    

    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):

    $query = "SELECT A.Agent_Name, C.Country_Name, J.Job_Type FROM Line_Items LI, Agents A, Country C, Job J WHERE LI.Agent_ID = 1 AND LI.Agent_ID = A.Agent_ID AND LI.Country_ID = C.Country_ID AND LI.Job_ID = J.Job_ID";
    $result = mysql_query($query);
    while($row = mysql_fetch_query($result)) {
        echo "Agent:" . $row['Agent_Name']."<br>";
        echo "Country:" . $row['Country_Name']."<br>";
        echo "Job:" . $row['Job_Type']."<br>";
    }
    

    Of course, you need to change the LI.Agent_ID if 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writng a small application in PHP + MySQL and have come to the
We have a layered application, or at least is in the process of transitioning
I've been developing a web application with PHP and MySQL. The other day, I
I am having trouble with modifying a php application to have pagination. My error
If you have geographic data stored in ESRI shapes, you have at least three
When I build a site, I'd like to have at least one browser I
I have seen at least one site in the past that had analytics on
I have a somewhat complicated branching structure at work (at least for me). It
I have few asynchronous tasks running and I need to wait until at least
Some background: In Germany (at least) invoice numbers have to follow certain rules: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.