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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:59:08+00:00 2026-05-16T07:59:08+00:00

i have 2 tables 1st table called services has id_service, name, date. description 2nd

  • 0

i have 2 tables

1st table called services has id_service, name, date. description
2nd table called services_images has id_img, img_name,id_service

Let’s say now that I have to, with one query (if possible), return 2 arrays

1st array with fields from one specific id from table "services"
2nd array with fields from all the images related to the selected id in “services” from table "services_images"

or better yet, only one array with the same data as said in 1, with an array INSIDE called “images” that has all the images listed from the table "services_images"

I need this to process and show data in a html page and this is the only way.

If I cant do it in mysql how can I arrange this in PHP, the only thing I can think of is 2 queries

Plus I’m always trying to improve my skills since I used to make single queries for everything, are there cases where making 1 query it’s just impossible?
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-05-16T07:59:08+00:00Added an answer on May 16, 2026 at 7:59 am

    For eaxmple with PDO:

    $db = new PDO($dsn, $user, $pass);
    
    $sql = 'SELECT services.id_service, services.name, services.date, services.description, services_images.id_image, services_images.img_name
    FROM services, services_images
    WHERE services.id_service = services_images.id_service
    ORDER BY services.id_service';
    
    $services = array();
    
    foreach($db->query($sql) as $row)
    {
       $serviceId = $row['id_service'];
    
       if(!array_key_exists($serviceId, $services))
       {
          $services[$serviceId] = array(
              'name' => $row['name'],
              'date' => $row['date'],
              'description' => $row['description'],
              'services_images' => array()
          );
       }
    
       $imgId = $row['id_img'];
       $services[$serviceId]['services_images'][$imgId] => array(
          'id_image' => $imgId,
          'img_name' => $row['img_name']
       ); 
    }
    
    print_r($services);
    

    Beware though.. if the two tables youre joining (in this case services and services_images) have any columns with the same name you will only get the value for the last one retrieved in the row unless you alias them or exclude them form your select statement.

    Additionally, if the size of your results is big you may have to use two queries similar to what Thomas has suggested because you may not have enough memory to hold the complete array structure.

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

Sidebar

Related Questions

I have 2 tables 1st table called services has id_service , name , date
In my database I have tables that define types for example Table: Publication Types
I have 3 tables Links Link ID Link Name GroupID (FK into Groups) SubGroupID
I have two tables that are joined together. A has many B Normally you
I have 2 tables. Table1 has fields A, B, C, D and Table2 has
I have a table called Basic , start_time is one field with type :VARCHAR(5),
I have a table like this: id | roll_no | name --------------------- 1 |
I have created an Oracle table with an indexed varchar2 column, called 'ID'. A
I have two tables. One is a table of the reports that have been
When I have tables in my database that have PK/FK relationships (int) and when

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.