所以首先让我解释一下我正在尝试做什么,然后我是如何做的,然后是问题所在。我正在尝试创建一个 mssql 查询来从数据库中读取发票。每张发票本质上是按发票 ID 组合在一起的各个费用的汇编。问题是不同的费用可能来自不同的容器(费用与容器相关)。我需要做的是按发票 ID 对费用进行分组,并将发票中所有费用的容器名称连接起来,并带有一些分隔符,如“|”。我创建了一个临时表@t,其中包含生成发票所需的所有费用和容器名称。在网上看了很长一段时间后,我发现 STUFF 命令似乎可以执行我想要的操作(出现并执行)。所以这是我的查询的 SQL:
select invoice_id, [Total Paid] RefNumCount,PayCount, MIN([rowNumber]) as rowNumber, Vendor, Due, [Invoice Number], Company, MAX(Location) as Location,
SUM([Funded Amount]) as [Funded Amount],
[Company ID], [Invoice ID], provider_reference_number, reconcile_date, all_providers
, STUFF(
(
select '|' + t1.[Container Name]
from @t t1
where t1.[Invoice Number] = t.[Invoice Number]
order by t1.[Container Name]
for XML PATH(''), TYPE
).value('.', 'nvarchar(100)')
,1,1,''
) as [Container Name]
from @t t
group by invoice_id, [Total Paid], RefNumCount, PayCount, Vendor, Due, [Invoice Number], Company,
[Company ID], [Invoice ID], provider_reference_number, reconcile_date, all_providers
当我运行这是 SQL Server Management Studio 2008 时,它运行得很好。但是,当我在我的 PHP 应用程序中使用此代码时,查询不起作用,最终我通过尝试读取一个空的结果集得到一个致命错误。有谁知道为什么会发生这个问题?或者如果有另一种连接字符串的方法?
以下是用于调用查询的 PHP 代码段
try{
$q = cms_database::instance();
$result = $q->query($sql_statement);
if (!$result) {
throw new Exception("Database Error [{$q->errno}] {$q->error}");
}
$invoices = array();
while($row=$result->fetch_assoc()) {
$invoiceX = array();
$invoiceX["rowNum"] = $row["rowNumber"];
$invoiceX["Vendor"] = $row["Vendor"];
$invoiceX["Due"] = $row["Due"];
$invoiceX["Acct"] = $row["Container Name"];//$row["Account Number"];
$invoiceX["container_id"]=$row["container_id"];
$invoiceX["provider_id"] =$row["provider_id"];
$invoiceX["Invoice"] = $row["Invoice Number"];
$invoiceX["Location"] = $row["Company"];
$invoiceX["FundedAmt"] = $row["Funded Amount"];
$invoiceX["InvoiceID"] = $row["Invoice ID"];
$invoiceX["Notes"] = $row["Notes"];
//merge the notes with notes from payment history that matches the invoiceid
$invoices[] = $invoiceX;
}
//filter out invoices with all reference numbers accounted for and remaining to be paid is $0.00
$returnPackage["data"] = $invoices;
//print $result;
} catch (Exception $e){
$returnPackage["success"]=0;
$returnPackage["message"]=$e.message;
}
我得到的错误是:“
致命错误:在第116行调用D:[path_to_the_file].php中的非对象上的成员函数 fetch_assoc() ”
第 116 行是代码行
while($row=$result->fetch_assoc()) {