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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:12:04+00:00 2026-05-27T16:12:04+00:00

I have made an SQL query from two tables. Everything works good but problem

  • 0

I have made an SQL query from two tables. Everything works good but problem is that this two tables have the same field names and after I do not know how to display them correct, how to tell that $data[‘aaa’] is from table 1 and the same $data[‘aaa’] from table 2

here is my SQL query :

    $query_str = "SELECT 
                    cm.id,
                    cm.global_category_id,
                    cm.num,
                    cm.menu_lv,
                    cm.menu_ru,
                    cm.menu_en,
                    u.id,
                    u.menu_lv,
                    u.menu_ru,
                    u.menu_en
                    FROM products_category cm, products_global_category u
                    WHERE cm.global_category_id = u.id
                    ";

and display data

<? foreach ($sub_category_list as $line) : ?>
<tr>
   <td><?=$line['menu_lv']?></td>   <---- here I want to display data from products_global_category u       
   <td><?=$line['sub_menu_lv']?></td>   <---- products_category cm
   <td><?=$line['sub_menu_ru']?></td>   <---- products_category cm
   <td><?=$line['sub_menu_en']?></td>   <---- products_category cm
</tr>
<? endforeach; ?>
  • 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-27T16:12:05+00:00Added an answer on May 27, 2026 at 4:12 pm

    As a solution you can change your SQL query to give an alias to the fields that have the same name.

    For example:

    SELECT somefield AS othername FROM table.
    

    In this case, the somefield field will be available through the alias othername.

    In your case:

    $query_str = "SELECT 
                    cm.id AS cm_id,
                    cm.global_category_id,
                    cm.num,
                    cm.menu_lv AS cm_menulv,
                    cm.menu_ru AS cm_menuru,
                    cm.menu_en AS cm.menuen,
                    u.id as u_id,
                    u.menu_lv AS u_menulv,
                    u.menu_ru AS u_menuru,
                    u.menu_en AS u_menuen
                    FROM products_category cm, products_global_category u
                    WHERE cm.global_category_id = u.id
                    ";
    

    And then in your PHP:

    $line['u_menulv'] //Access field menu_lv from products_global_category table
    $line['cm_menulv'] //Access field menu_lv from products_category table
    

    EDIT: In mysql_fetch_array documentation page:

    If two or more columns of the result have the same field names, the
    last column will take precedence. To access the other column(s) of the
    same name, you must use the numeric index of the column or make an
    alias for the column. For aliased columns, you cannot access the
    contents with the original column name.

    In other words, either create an alias like shown above or access the fields by the numeric index of the array.

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

Sidebar

Related Questions

I have made an SQL query that picks out web statistics from a database.
I have two tables that contain data from different temperature sensors: Table1 _TimeStamp Temperature
I have a very simple SQL query that I made which retrieves some user
i have made columns in some of the tables encrypted in sql server 2008.
I am returning two lists from the database using LINQ to SQL compiled query.
I have two tables tblActionLog and tblLoginLog . This is tblActionLog : tblActionLog ------------
I don't know if this is a dumb question but I have this two
I have the following SQL query but it doesn't entirely work because it shows
In my model I've made this custom sql query : SELECT training_courses.id, training_courses.training_id, training_courses.course_id,
I have a SQL query and it fails at times. That means that query

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.