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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:27:25+00:00 2026-06-16T01:27:25+00:00

I have 1 table on the database :( SQL ) — id — city_name

  • 0

I have 1 table on the database 🙁 SQL )

-- id
-- city_name
-- image

and I want to view every images inside them city, for example : USA has 3 images and Canada has 2, it will be looks like :

USA
image 01
image 02
image 03

CANADA
image 01
image 02

I tried this code, it will be view all the records mixed

$results1 = mysql_query("SELECT * FROM gallery");
while($r = mysql_fetch_array($results1)) {

    echo $r[city_name]."<hr>";

    $city = $r[city_name];
    $results2 = mysql_query("SELECT * FROM gallery WHERE city_name='$city'");


    while($r = mysql_fetch_array($results2)){
        echo '<img border="0" src="'.$r['image'].'">';
    }
}

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-16T01:27:26+00:00Added an answer on June 16, 2026 at 1:27 am

    Correction in your code:

    You are overwriting `result set $r of first query into second while loop

        $last='';
        $results1 = mysql_query("SELECT * FROM gallery order by city_name ASC");
        while($r = mysql_fetch_array($results1)){
    
        if($last!=$r['city_name']) {    
        echo $r['city_name']."<hr>";
           $last = $r['city_name']; 
        }
    
        $city = $r['city_name'];
    
        $results2 = mysql_query("SELECT * FROM gallery WHERE city_name='$city'");
    
    
            while($r1 = mysql_fetch_array($results2)){ //change this variable
    
                        echo $r1['image'];
    
            }
       }
    

    Second solution:

    $data = array();
    $results1 = mysql_query("SELECT * FROM gallery");
    while($r = mysql_fetch_array($results1)){
        $data[$r['city_name']][]=$r;
    }
    foreach($data as $key=>$images){
         echo $key.PHP_EOL; //city name
         foreach($images as $image){
             echo $image['id'].PHP_EOL; // each image id
             echo $image['image'].PHP_EOL; // each image for city
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a scenario, i want to monitor database(SQL Server) table, if record is
database/SQL novice here. I have a table with a column called, for example, EmployeeID.
I have an SQL database table, I want to find everything in a table
In Microsoft SQL database, I have a table where every column have default values
I have a SQL database table with User info - ID, Name, Address, City,
I have an empty Table in SQL Server 2008 Database, I want to brink
I have a table in SQL database: ID Data Value 1 1 0.1 1
I have a table like this in my database (SQL Server 2008) ID Type
I have a table with data from an SQL database and the table exists
I have to pull data from a SQL database table to my DB2 table.

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.