如何使用php从两个表信息中获取数据

时间:2012-03-21 06:11:22

标签: php mysql

我想从两个表中获取数据。我在一张桌子上有耐心的名字和姓氏,在其他表格中有预约时间。我使用以下方式,但它出错:

SELECT    
    PatientMaster.PatientFirstName,
    PatientMaster.PatientLastName,
    ProviderAppointmentListings.AppointmentTime 
FROM PatientMaster 
    JOIN ProviderAppointmentListings 
    ON PatientMaster.PatientID = ProviderAppointmentListings.PatientID


$result = mysql_query($query) OR die(mysql_error());
$rows = array();
while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
echo json_encode($rows);

它在这里打印查询空

3 个答案:

答案 0 :(得分:0)

试试这个:

$query = "SELECT ... ";
$result = mysql_query($query);//<=== add this line
$rows = array();
while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
echo json_encode($rows);

答案 1 :(得分:0)

<强> [EDITED]

尝试:

$query="SELECT PatientMaster.FirstName,PatientMaster.LastName,ProviderAppointmentListings.AppointmentTime 
FROM PatientMaster JOIN ProviderAppointMentListings ON PatientMaster.PatientID = ProviderAppointmentListings.PatientId";

$result = mysql_query($query) or die(mysql_error());

如果出现MySQL错误,那么您的查询肯定有问题。

答案 2 :(得分:0)

你能复制/粘贴确切的错误信息吗?还可以尝试这个简单的JOIN查询:

SELECT    
    PatientMaster.PatientFirstName,
    PatientMaster.PatientLastName,
    ProviderAppointmentListings.AppointmentTime 
FROM PatientMaster, ProviderAppointmentListings
WHERE 
    PatientMaster.PatientID = ProviderAppointmentListings.PatientID