2013-03-31 133 views
0

我需要使用內部聯接來顯示來自另一個表的數據。SQL內部聯接

$pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid"; 
    SELECT Doctor_ID 
    FROM Appointment 
    INNER JOIN Doctor 
    ON Appointment.Doctor_id=Doctor.Doctor_id 

目前我有一個表顯示數據,但我也需要使用內部連接顯示來自另一個表的數據。我如何將創建的SELECT代碼插入到我當前的編碼中?醫生的詳細信息是我正在嘗試使用醫生ID在我的頁面中輸出的內容。我是新來的PHP。

謝謝

完整的PHP代碼appointment.php

<?php 
{ 
    mysql_connect("localhost" , "" , "") or die (mysql_error()); 
    mysql_select_db("") or die(mysql_error()); 


    $pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid"; 
    SELECT Doctor_id 
    FROM Appointment 
    INNER JOIN Doctor 
    ON Appointment.Doctor_id=Doctor.Doctor_id 

    //executes query on the database 
    $result = mysql_query ($query) or die ("didn't query"); 
    //this selects the results as rows 

    $num = mysql_num_rows ($result); 
    //if there is only 1 result returned than the data is ok 
    if ($num == 1) {} 
    { 
     $row=mysql_fetch_array($result); 
     $_SESSION['Appointment_date'] = $row['Appointment_date']; 
     $_SESSION['Appointment_time'] = $row['Appointment_time']; 

    } 
} 
?> 

     <strong>Dates available</strong>    
     <select id="Availability" name="Availability">      
     <option value="0">--Select date--</option> 
     <option value="1"><?php echo $_SESSION['Appointment_date'];?></option> 
     </select> 

     <br /> 
     <br /> 

     <strong>Times available</strong>    
     <select id="Availability" name="Availability">      
     <option value="0">--Select time--</option> 
     <option value="2"><?php echo $_SESSION['Appointment_time'];?></option>> 
     </select> 

全部代碼行從57至98

+0

看起來好像你有你需要的所有信息在你的問題。你卡在哪裏? – SomeSillyName

+0

它顯示一個語法錯誤。我有沒有留下什麼? – user2216325

+0

這是我得到的錯誤。解析錯誤:語法錯誤,/web/stud中出現意外的T_STRING // 64行上的PHP/appointment1.php – user2216325

回答

0

我同意上司的評價。試試這個(新的查詢語法...)

<?php 
{ 
mysql_connect("localhost" , "" , "") or die (mysql_error()); 
mysql_select_db("") or die(mysql_error()); 


$pid=intval($_SESSION["Patient_id"]); 
$query = "SELECT Apointment.Apointment_id, Apointment.Doctor_id, Apointment.Patient_id, Apointment.Time 
      FROM (Doctor INNER JOIN Apointment ON Doctor.Doctor_id = Apointment.Doctor_id) INNER JOIN Patient ON Apointment.Patient_id = Patient.Patient_id 
      WHERE Apointment.Patient_id='". $pid . "'"; 

//executes query on the database 
$result = mysql_query ($query) or die ("didn't query"); 
//this selects the results as rows 

$num = mysql_num_rows ($result); 
//if there is only 1 result returned than the data is ok 
if ($num == 1) {} 
{ 
$row=mysql_fetch_array($result); 
$_SESSION['Appointment_date'] = $row['Appointment_date']; 
$_SESSION['Appointment_time'] = $row['Appointment_time']; 

} 
} 
?>