Mysql php - select fields from 2 tables with the same field names

So, I have a custom CMS that allows you to dynamically create forms and lists, etc. I noticed a problem when it captures data for a list, which contradicts the approval table in the database.

The problem is that if the data table has field names the same as the field names in the claims table, then when I mysql_fetch_array and returns the values ​​in the array, it returns only one field name

So, an example of what returns

Array ( [id] => 1 ) 

And ideally, I would like him to come back as

 Array ( [approvals.id] => 1 [affiliates.id] => 2 ) 

So, how can I force the table name prefix to an array of results to counteract the names of the fields that are called the same? I do not want to change the names of the fields as they are pretty built-in.

+4
source share
4 answers

Usually you use an alias in an SQL statement:

 SELECT table1.id as t1_id, table2.id as t2_id FROM ..... 

Then, when you have a selection, you will access it as follows:

 echo $row['t1_id']; 
+4
source

Use mysql alias

claim table alias id as [id AS approvals.id]

branch table alias id as [id AS affiliates.id]

+1
source

Use alias in request:

 SELECT approvals.id AS approvals_id, affiliates.id AS affiliates_id ... 

Then the associative array will contain:

 echo $row['approvals_id']; 
+1
source

The problem is that if the data table has the same field names as the field names in the claims table

You need an alias for the names of these fields with different aliases. Sort of:

 SELECT approvals.id approvalsid, affiliates.id affiliatesid ... FROM ... 
+1
source

Source: https://habr.com/ru/post/1447444/


All Articles