2016-01-21 94 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

查询输出在一个字段值中有彗形(,),当我删除此列时,它运行良好。 –