在php中查询执行错误

时间:2016-01-21 12:22:13

标签: php sql-server-2005

我正在使用php和后端sql server 2005创建一个报告。我在sql server中编写了这个运行良好的查询,同时在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为资源,在第65行的C:\ wampnew \ www \ logs \ viewTodaysCollection.php中给出布尔值

1 个答案:

答案 0 :(得分: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);