【发布时间】: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?
标签: php sql sql-server sqlsrv php-mssql