简体   繁体   中英

selecting a particular field from a table?

I am using the following SQL query:

Select * from table1 as t1, table2 as t2 where t1.id = t2.col

but my problem is that both the tables have fields with same name, place . So how can I select the column with name place from table2 in my PHP code? I want to use the following php code

 while($row_records = mysql_fetch_array($result_records))
    {

            <?  echo $row_records['place']; ?>

     }

How can I fetch the field from particular table?

Never use...

Select * from ...

... in a production environment - Always specify explicitly which columns you want to return.

Thus you could amend your SQL to:

Select t1.Place as T1Place, t2.Place as T2Place
  from table1 as t1, table2 as t2 where t1.id = t2.col

So in your PHP you would have:

 while($row_records = mysql_fetch_array($result_records))
 {

        <?  echo $row_records['T2Place']; ?>

 }

Why don't you use the table alias and the field name. For example,

    Select t1.place as t1_place, t2.place as t2_place 
      from table1 as t1, table2 as t2 where t1.id = t2.col

In your PHP code you can select it using

while($row_records = mysql_fetch_array($result_records))
    {
    echo $row_records['t1_place']; 
    echo '<br />';
    echo $row_records['t2_place']; 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM