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 7997483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:58:32+00:00 2026-06-04T14:58:32+00:00

I have two table classifieds and state . classifieds – id, title, state_id state

  • 0

I have two table classifieds and state.

classifieds – id, title, state_id

state – id, statename

I am trying to echo out the statename from state. I’ve tried this query but it echos only the last row.

    <?php
    $query  = "SELECT * FROM classifieds ";
    $result = mysql_query($query) or die(mysql_query());
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
   ?>

    <div>
   <span>Title: <?php echo $row['title'];?> </span>
    </div>

   <div>
   <span>Name: <?php echo $row['name'];?> </span>
    </div>

      $q = mysql_query("SELECT * FROM classifieds AS C
    LEFT JOIN  state AS S
    ON C.state_id = S.id");
    $z = array();

    while($state=mysql_fetch_array($q))
     {
      array_push($z,$state["statename"]);
     }

     ?>
    <div>
    <span>State: <?php foreach($z as $location) { echo $location; } ?></span>

    </div>

  <?php } // End of the first while loop ?>

Thanks

  • 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-04T14:58:33+00:00Added an answer on June 4, 2026 at 2:58 pm

    You need to move your echo command inside the while loop. Otherwise it will only echo once it has gone through all of the rows.

     <?php
     $q = mysql_query("SELECT * FROM classifieds AS C
    LEFT JOIN  state AS S
    ON C.state_id = S.state_id");
     while($state=mysql_fetch_array($q)){
        echo "Location: ".$state["statename"];
     }
     ?>
    

    Edit: With your updated code, I can see that your problem is that you first get a list of all the classifieds, then that same list again with the states joined in. You don’t need to do the first query at all, only the second one.

     <?php
     $q = mysql_query("SELECT C.*, S.statename 
           FROM classifieds AS C
           LEFT JOIN  state AS S
           ON C.state_id = S.state_id");
     while($row=mysql_fetch_array($q)){
        echo "Title: ".$row["title"]."<br />";
        echo "Name: ".$row["name"]."<br />";
        echo "Location: ".$state["statename"]."<br />";
     }
     ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have two table like this job(id,title,location,...) job_page(id,id_job,id_page,page_name) For example I want to write
I have two table category and subcategory I am trying to delete record from
I have two table with different column like this: table1 ( _id, title, name,
I have two table like this: table1_ride -------- id ride id from_which_city city id
I have two table. OLDTABLE and NEWTABLE. I need to insert some data from
I have two table views displaying the data from a Sqlite database. I can
I have two table and I should write a select query which join this
I have two table view filled in their viewDidLoad with data coming from a
I have two table. user and address . I am joining them like this

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.