PHP 5.4 到 7.4 mssql 到 sqlsrv sql 查询未运行
Posted
技术标签:
【中文标题】PHP 5.4 到 7.4 mssql 到 sqlsrv sql 查询未运行【英文标题】:PHP 5.4 to 7.4 mssql to sqlsrv sql query not running 【发布时间】:2021-04-29 12:24:53 【问题描述】:前言 我是一年级的系统管理员,我一直在迁移服务器以使这家公司保持最新状态。我遇到了运行 php 5.4 的服务器,我正在尝试将其移至 php 7.4。一切最初都是用 mssql 编写的,我将其移至 sqlsrv。我有连接工作。我可以成功执行一些查询,但我无法让这些为 mssql 编写的查询与 sqlsrv 一起使用。
我知道的事情:
$record
以数组的形式返回,我使用 gettype 对其进行了测试。
$conn
有效,因为我可以通过简单的查询从数据库中查询表。
sql 查询在 sql server 中正确运行。
任何建议都将不胜感激,因为我觉得我需要重写整个脚本,因为我已经为此苦苦挣扎了几天。下面的 sn-p 只是 if else
链中内置的众多查询之一。
php 5.4 编写的原始代码:
$query = "Select * INTO #temptable FROM (
SELECT Employee
,transdate
,sum([RegHrs]) as RegHrs
,sum([OvtHrs]) as OvtHrs
FROM [dbo].[tkDetail]
WHERE TransDate >= cast('" . $startdate . "' as datetime) and TransDate <= cast('" . $enddate . "' as datetime)
GROUP BY Employee, transdate) as x
SELECT LastName,FirstName,emmain.Employee, emcom.PayType, cast(data.TransDate as date) as TransDate
,data.reghrs
,data.ovthrs
from dbo.emmain emmain
Left Join #temptable data on emmain.employee = data.employee
Left Join dbo.EMCompany emcom on emmain.employee = emcom.employee
Left Join dbo.EmployeeCustomTabFields custom on emmain.employee = custom.employee
WHERE custom.custFullTimePartTime = 'Full Time' and emcom.status = 'A'
";
$result = mssql_query($query);
while ( $record = mssql_fetch_array($result) )
// BUGFIX: Salary reporting shows no regular hour entry as null instead of 0.0
if ($record['reghrs'] == null)
$record['reghrs'] = "0.0";
// BUGFIX: Salary reporting shows no overtime hour entry as null instead of 0.0
if ($record['ovthrs'] == null)
$record['ovthrs'] = "0.0";
if (($record['reghrs'] + $record['ovthrs'] <= 4) && ($record['reghrs'] + $record['ovthrs'] > -1))
print "\t\t\t\t\t<tr>\n";
print "\t\t\t\t\t\t<td>" . $record['Employee'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['FirstName'] . " " . $record['LastName'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['PayType'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . number_format((float) $record['reghrs'], 3) . "</td>\n";
print "\t\t\t\t\t\t<td>" . number_format((float) $record['ovthrs'], 3) . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['TransDate'] . "</td>\n";
print "\t\t\t\t\t</tr>\n";
$reccount += 1;
我尝试做的事情:
$query = "Select * INTO #temptable FROM (
SELECT Employee
,transdate
,sum([RegHrs]) as RegHrs
,sum([OvtHrs]) as OvtHrs
FROM [dbo].[tkDetail]
WHERE TransDate >= cast('" . $startdate . "' as datetime) and TransDate <= cast('" . $enddate . "' as datetime)
GROUP BY Employee, transdate) as x
SELECT LastName,FirstName,emmain.Employee, emcom.PayType, cast(data.TransDate as date) as TransDate
,data.reghrs
,data.ovthrs
from dbo.emmain emmain
Left Join #temptable data on emmain.employee = data.employee
Left Join dbo.EMCompany emcom on emmain.employee = emcom.employee
Left Join dbo.EmployeeCustomTabFields custom on emmain.employee = custom.employee
WHERE custom.custFullTimePartTime = 'Full Time' and emcom.status = 'A'
";
$result = sqlsrv_query($conn, $query);
if( $result )
echo "result true";
else
echo "result false <br />";
die( print_r( sqlsrv_errors(), true));
while ($record = sqlsrv_fetch_array($result))
// BUGFIX: Salary reporting shows no regular hour entry as null instead of 0.0
if ($record["reghrs"] == null)
$record["reghrs"] = "0.0";
// BUGFIX: Salary reporting shows no overtime hour entry as null instead of 0.0
if ($record['ovthrs'] == null)
$record['ovthrs'] = "0.0";
if (($record['reghrs'] + $record['ovthrs'] <= 4) && ($record['reghrs'] + $record['ovthrs'] > -1))
print "\t\t\t\t\t<tr>\n";
print "\t\t\t\t\t\t<td>" . $record['Employee'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['FirstName'] . " " . $record['LastName'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['PayType'] . "</td>\n";
print "\t\t\t\t\t\t<td>" . number_format((float) $record['reghrs'], 3) . "</td>\n";
print "\t\t\t\t\t\t<td>" . number_format((float) $record['ovthrs'], 3) . "</td>\n";
print "\t\t\t\t\t\t<td>" . $record['TransDate'] . "</td>\n";
print "\t\t\t\t\t</tr>\n";
$reccount += 1;
【问题讨论】:
日志中有错误吗? “没有运行”并不是一个很好的描述 很久没接触MSSQL了。我看到两个查询,是否有可能需要使用sqlsrv_next_result($result)
跳过第一个?您可能还应该在调试期间检查sqlsrv_rows_affected($result)
克里斯哈斯!那是问题!感谢您的提示,我不认为它们是两个单独的查询,也从来不知道 sqlsrv_next_result。
为什么不切换到 PDO?
【参考方案1】:
我已经开始使用PDO
,但我的一台服务器仍在使用sqlsrv
功能。当我从 mssql
函数转换时,我遇到了同样的麻烦。
您可以使用sqlsrv_next_result
函数移动指针(如上面的提示),或者您可以简单地提供一个附加参数来强制将结果作为关联数组返回。我发现标准的 while
循环就像旧函数一样工作,当它以这种方式完成时。
代替:
while ($record = sqlsrv_fetch_array($result))
用途:
while ($record = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
【讨论】:
以上是关于PHP 5.4 到 7.4 mssql 到 sqlsrv sql 查询未运行的主要内容,如果未能解决你的问题,请参考以下文章
使用 SQL Server 2012 中的 MSSQL_Query 的 PHP 调用存储过程返回无效值