我似乎遇到了问题。我继承了一些在Windows机器上运行的PHP代码,它连接到同一台机器上的MSSQL DB。不幸的是,我们是一个Linux家,我们现在需要在Linux机器上运行此代码。因此,我正在慢慢地通过这一切并进行调整以确保它在Linux中正确运行。大多数更改都是为了满足C:\而不是/ var / www和变量检查的配置设置。
虽然我碰到了一堵砖墙。代码运行一个查询,返回有限的分页结果。
php代码如下所示:
if ( $offset !== null ) {
$querystring = "exec SSDEV.dbo.sp_limit '".str_replace("'", "''",$querystring)."', '".intval($offset)."','".intval($limit)."';";
}
查询字符串最终看起来像这样:
exec SSDEV.dbo.sp_limit 'select users.* from users order by users.first_name asc', '0','20';
使用以下代码行运行查询:
$result = mssql_query($querystring,$this->connection);
sp_limit读取如下:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
ALTER procedure sp_limit (@sql text, @offset int, @limit int)
as
declare @cursor int;
set @offset=@offset+1;
exec sp_cursoropen @cursor output, @sql, 8,8193;
exec sp_cursorfetch @cursor,32,@offset,@limit;
exec sp_cursorclose @cursor;
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
在查询分析器中运行存储过程,它运行没有任何问题,但我注意到它返回2个结果集,一个空白一个(虽然有列字段),sp_cursoropen和一个来自sp_cursorfetch,其中包含数据I'我正在寻找。
如果我注释掉在PHP中创建存储过程查询字符串的行,并且只运行没有“限制”的基本SQL语句,则代码运行正常。
但是,当它与存储过程一起运行时,页面将永久挂起,然后在mssql_query()函数上失败。以下是apache日志中的错误:
[Wed Sep 26 09:26:34 2012] [error] [client 127.0.0.1] PHP Warning: mssql_query(): Query failed in /var/www/mysite/libs/DB.class.php on line 80, referer: http://mysite/
[Wed Sep 26 09:26:34 2012] [error] [client 127.0.0.1] PHP Warning: mssql_query(): Unable to set query in /var/www/mysite/libs/DB.class.php on line 80, referer: http://mysite/
[Wed Sep 26 09:26:34 2012] [error] [client 127.0.0.1] PHP Fatal error: Uncaught exception 'Exception' with message 'Query failed: Changed database context to 'SSDEV'.' in /var/www/mysite/libs/DB.class.php:82
Stack trace:
#0 /var/www/mysite/libs/HTML.class.php(22): DB->select('select region_c...', 2, 1)
#1 /var/www/mysite/controllers/SS_Users_Controller.class.php(264): HTML->get_select_query('select region_c...', 'region_code', 'region_name', 'fk_regions', '', ' a Region', Array)
#2 /var/www/mysite/libs/Controller.class.php(72): SS_Users_Controller->index()
#3 /var/www/mysite/index.php(34): Controller->execute()
#4 {main}
thrown in /var/www/sweetspot/libs/DB.class.php on line 82, referer: http://mysite/
[Wed Sep 26 09:26:34 2012] [error] [client 127.0.0.1] PHP Notice: Unknown: Skipping numeric key 0 in Unknown on line 0, referer: http://mysite/
如您所见,它在执行存储过程时遇到问题。据我所知,在PHP中返回多个结果集并处理它不应该是一个太大的问题。如果在尝试访问结果集时抛出错误,那么我会更容易相信它有两个结果集的事实,但是从我的谷歌搜索,我知道PHP不应该有它的问题,除了你需要使用mssql_next_result()。什么问题,这是在Windows版本上工作正常,为什么它突然出现问题,为什么只有存储过程而不是正常的SQL语句?
非常感谢任何帮助。