php如何在$ row中指定表

时间:2014-11-08 17:49:28

标签: php mysql

假设以下PHP代码:

$query="SELECT USERS.mail,EXPERTS.mail FROM USERS,EXPERTS WHERE USERS.ID<=10 AND 
EXPERTS.FK_ID=USERS.ID";            
$result=$db->query($query);        
if ($result->num_rows>=1) 
{       
  $row=$result->fetch_assoc(); 
  $data=$row["mail"];   
}

$ row中的“mail”列对应于表USERS或表EXPERTS?我该如何选择所需的桌子?我试过$ data = $ row [“USERS.mail”];它不起作用......

非常感谢

2 个答案:

答案 0 :(得分:3)

在查询中使用别名:

SELECT USERS.mail AS u_mail, EXPERTS.mail AS e_mail ...

然后,您可以访问$row['u_mail']$row['e_mail']

答案 1 :(得分:0)

你可以这样做:

$query="SELECT USERS.mail as users_mail,EXPERTS.mail as experts_mail 
        FROM USERS,EXPERTS 
        WHERE USERS.ID<=10 AND EXPERTS.FK_ID=USERS.ID";            
$result=$db->query($query);        
if ($result->num_rows>=1) 
{       
  $row=$result->fetch_assoc(); 
  $data=$row["users_mail"]; 
  // or
  // $data=$row["users_mail"]; 
}

它使用标识符(或别名)http://dev.mysql.com/doc/refman/4.1/en/identifiers.html

table.column AS identifier