【问题标题】:query execution error in phpphp中的查询执行错误
【发布时间】:2016-01-21 12:22:12
【问题描述】:

我正在使用 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);

这是Error给出的错误

警告:sqlsrv_fetch_array() 期望参数 1 是资源,布尔值在 C:\wampnew\www\logs\viewTodaysCollection.php 第 65 行给出

【问题讨论】:

  • 请您先检查一下您的 sqlsrv_connect 好吗?你的连接完成了吗?
  • sqlsrv_query 可能返回了错误。在fetch_array 中使用$stid 之前检查它的返回值。 (可能你的$query有错误)
  • 使用var_dump打印出来检查$stid的值,你会看到它是布尔类型
  • 是的,我检查了 $stid 并返回错误,即数组
  • 由 var_dump 检查并返回 bool(false)

标签: php sql-server-2005


【解决方案1】:
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);

【讨论】:

  • 查询输出在一个字段值中有逗号(,),当我删除此列时,它运行正常。
猜你喜欢
  • 2012-12-29
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多