2016-01-21 82 views
0

我正在用php和後端sql server 2005創建一個報告。我在sql服務器上寫了這個運行正常的blew query,同時在php中執行時給出錯誤。php中的查詢執行錯誤

$query = "select distinct t1.VisitDate,t1.ReceiptNo,t1.VisitorNo,p.PatientName,STUFF((
    SELECT f.Particular + ', ' from CollectionPointTable t2,FeesTable f 
    where t1.ReceiptNo = t2.ReceiptNo and f.ID = t2.TestID 
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)') ,1,0,'') Tests , sum(t1.payment) as Amount 
    from CollectionPointTable t1,patientinformationtable p 
    where p.visitorno = t1.visitorno and convert(varchar(10),t1.VisitDate,103) = '$date' 
    group by t1.Receiptno,p.patientname,t1.VisitorNo,t1.VisitDate 
    order by ReceiptNo"; 

$stid = sqlsrv_query($conn, $query); 
$Today_patient_list = sqlsrv_fetch_array($stid,SQLSRV_FETCH_ASSOC); 

即其通過錯誤給出

警告該錯誤:sqlsrv_fetch_array()預計參數1是資源,在布爾C中給出:\ wampnew \ WWW \日誌\ viewTodaysCollection.php上線65

+1

請問您首先檢查您的sqlsrv_connect?你的連接是否完成? – KinjalMistry

+1

'sqlsrv_query'可能返回了一個錯誤。在'fetch_array'中使用'$ stid'之前檢查它的返回值。 (也許在你的'$ query'中有錯誤) – Pierre

+0

用'var_dump'打印出來檢查'$ stid'的值,你會發現它是布爾類型 –

回答

0
Use the following code, it should work: 
$query = "select distinct t1.VisitDate,t1.ReceiptNo,t1.VisitorNo,p.PatientName,STUFF((
    SELECT f.Particular + ', ' from CollectionPointTable t2,FeesTable f 
    where t1.ReceiptNo = t2.ReceiptNo and f.ID = t2.TestID 
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)') ,1,0,'') Tests , sum(t1.payment) as Amount 
    from CollectionPointTable t1,patientinformationtable p 
    where p.visitorno = t1.visitorno and convert(varchar(10),t1.VisitDate,103) = '$date' 
    group by t1.Receiptno,p.patientname,t1.VisitorNo,t1.VisitDate 
    order by ReceiptNo"; 

$stid = sqlsrv_query($conn, $query); 
$Today_patient_list = sqlsrv_fetch_array($stid); 
+0

仍然是相同的錯誤 –

+0

查詢輸出在一個字段值中有彗形(,),當我刪除此列時,它運行良好。 –