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

  • SEARCH
  • Home
  • 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 8487905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:22:58+00:00 2026-06-10T21:22:58+00:00

I am very new to php and mysql. I am trying to learn as

  • 0

I am very new to php and mysql. I am trying to learn as I go and am stuck on this issue.

I have a many-to-many relationship. The tables are: users and state. Many companies in the users table operate in many states and each state has many different companies. Each company on the user table has a unique id that is stored on the state table. The state table then has a column for the state name that the company operates in. A new row is created in the state table for each state the company is in. That all seems to work ok.

I wrote the following php code to pull all the company id’s from the state table for any given state ($state) and then display all the company names (pulled from the users table with the company id) that operate in the given state.

My problem is that I would like to order the list alphabetically of companies that operate in any given state. This code will not do that. Can anyone offer a better way to do this that will allow the list to be ordered (or that will just generally be more efficient and better)?

$state=$_GET['state'];
echo $state;
$aid=array();
$result=mysql_query("SELECT * FROM state WHERE state='$state'");
while($row=mysql_fetch_assoc($result)){
$v=$row['company_id'];
array_push($aid,$v);};

foreach($aid as $val){
$result1=mysql_query("SELECT * FROM users WHERE company_id='$val'");
$row1=mysql_fetch_assoc($result1);
$company=$row1['company'];
echo $company.'<br/>';};

The table structures are:

users

company_id
company
(other columns that are not relevant)

state

index
company_id
state
(no additional columns)
  • 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-06-10T21:22:59+00:00Added an answer on June 10, 2026 at 9:22 pm

    Try this SQL statement:

    SELECT 
        s.state, u.company 
    FROM 
        states s, users u 
    WHERE 
        s.company_id = u.company_id 
    AND 
        s.state = "$state" 
    GROUP BY 
        s.state 
    ORDER BY 
        s.state, u.company
    

    This should give you an alphabetic list of companies per state selected. The ORDER BY uses the state first in case you decide to expand your query to get more than one states.

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

Sidebar

Related Questions

I am relatively new to MySQL and PHP and I have been trying to
im very new to this and have a very basic knowledge php and SQL
I am very new to PHP and trying to select data from a MySQL
i am very new to PHP and MySQL. I am trying to write a
I am very new to PHP and MySql. Is there any method by which
I'm very new to PHP and I have a small task of displaying a
I am very new to php, and I am trying to develop a facebook
I am pretty new to both php and mysql. I have a txt file
I am very new to OOP styled PHP, and I am trying to implement
Actually I am very new to PHP. My code goes here: <?php $mysql =

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.