Reputation: 3308
Here is my query:
$query = 'SELECT * extensa_econt_city as econt_city, extensa_econt_office as econt_office WHERE econt_city.city_id = econt_office.city_id';
I am trying to select all the rows from table extensa_econt_city
and extensa_econt_office
where city_id
from extensa_econt_city
is equal to city_id
from extensa_econt_office
.
Then when i get this information i have to create FORECH loop. It is important to be FORECH not while loop.
Here is my sample code:
<?PHP
foreach($results as $row){
echo $row['econt_city.name'];
echo $row['econt_office.name'];
}
?>
In this foreach loop i have to select row name
from extensa_econt_office
and extensa_econt_city
.
I hope you understand what i am asking.
Can you help me out resolve this problem.
Thanks in advance!
Upvotes: 1
Views: 44
Reputation: 1492
I am trying to select all the rows from table extensa_econt_city and extensa_econt_office where city_id from extensa_econt_city is equal to city_id from extensa_econt_office... In this foreach loop I have to select row
name
fromextensa_econt_office
andextensa_econt_city
.
SELECT eec.name AS ee_city_name,
eeo.name AS ee_office_name,
FROM extensa_econt_city eec
LEFT JOIN extensa_econt_office eeo ON eec.city_id = eeo.city_id
And in the PHP foreach loop, you can access the data like this:
<?php
foreach($results as $row){
echo $row['ee_city_name'];
echo $row['ee_office_name'];
}
Upvotes: 2
Reputation: 24959
All you are looking for is the name
column, not all columns (select *) from what you have shown. Don't drag down your system with wildcards as such (with select *). Also do an explicit join.
Here is a visual of my pretend schema, as I am not suggesting it is yours.
Do an explicit join, unlike your join in a where clause (that style is circa 1995)
create table extensa_econt_city
( id int auto_increment primary key,
city_id int not null,
name varchar(100) not null
);
create table extensa_econt_office
( id int auto_increment primary key,
city_id int not null,
name varchar(100) not null
);
insert extensa_econt_city(city_id,name) values (1,'eec name1'),(2,'eec name2'),(3,'eec name3');
insert extensa_econt_office(city_id,name) values (1,'EEO name1'),(2,'EEO name2'),(3,'EEO name3');
select eec.name as eec_name,eeo.name as eeo_name
from extensa_econt_city eec
join extensa_econt_office eeo
on eeo.city_id=eec.city_id;
+-----------+-----------+
| eec_name | eeo_name |
+-----------+-----------+
| eec name1 | EEO name1 |
| eec name2 | EEO name2 |
| eec name3 | EEO name3 |
+-----------+-----------+
Upvotes: 2
Reputation: 33813
I think this might do what you mean.
select
c.`city_id`,
c.`post_code`,
c.`type`,
c.`name`,
c.`name_en`,
c.`zone_id`,
c.`country_id`,
c.`office_id`
o.`FIELD_NAME` as 'ALIAS',
o.`NEW_FIELD_NAME` as 'OTHER ALIAS'
from `extensa_econt_city` c
left outer join `extensa_econt_office` o on o.`city_id`=c.`city_id`
Once the tables are aliased ( in this case the aliases are c & o ) you can select either all records using * ( unless there are duplicated names in which case you would get an error ) or, as above, select the desired fields directly in the query.
Once the query is set the FOREACH
loop can select he record by the name / alias defined in the sql
Upvotes: 2