简体   繁体   English

如何从PHP Codeigniter中的临时表从MSSQL存储过程获取结果

[英]How to get result from MSSQL stored procedure with temp table in PHP Codeigniter

I am using Codeigniter 3.1 and I wanna get result from a stored procedure in Microsoft SQL Server. 我正在使用Codeigniter 3.1,我想从Microsoft SQL Server中的存储过程中获取结果。 but I can't get any result 但我没有任何结果

Stored Procedure Script: 存储过程脚本:

ALTER procedure [dbo].[SendPost_All_Periodic]
@DateAs nvarchar(10),
@DateTo nvarchar(10)   
as
begin


 DECLARE @temptable TABLE (MsgDate nvarchar(10),Saat tinyint)

 INSERT INTO @temptable
 SELECT MsgDate,CAST(LEFT(MsgTime, 2) AS int) as Saat2
 FROM  dbo.TMessage
 Where MsgDate>=@DateAs and MsgDate<=@DateTo

 Select Saat,count(Saat)as CountSend from @temptable
 group by Saat
 order by Saat

end

My PHP Code: 我的PHP代码:

$DateAs = "2017/01/02";

$DateTo = "2017/01/03";

$result = $this->db->query("SendPost_All_Periodic '{$DateAs}', '{$DateTo}'");

print_r($publishtime->result_array());

But when I use from this method for normal select, This code create result, but I can't get result than select from TempTable 但是,当我使用此方法进行常规选择时,此代码将创建结果,但是除了从TempTable中进行选择外,我无法获得结果

Are you getting any error messages when you try to execute your code? 尝试执行代码时收到任何错误消息吗?

Based on the code you have provided, you might have a few problems and the call might depend upon which database driver you are using in your config (ex: pdo). 根据您提供的代码,您可能会遇到一些问题,并且调用可能取决于您在配置中使用的数据库驱动程序(例如:pdo)。 First, in your print_r statement, you are calling a variable that seemingly doesn't exist. 首先,在您的print_r语句中,您正在调用一个看似不存在的变量。 So, give this a quick try: 因此,快速尝试一下:

print_r($result->result_array());

Also, the way you are passing in your parameters might cause an issue (the single quotes). 另外,传递参数的方式可能会引起问题(单引号)。 Give these both a try and see which works. 尝试一下,看看哪个可行。

Method One: 方法一:

$result = $this->db->query("SendPost_All_Periodic {$DateAs}, {$DateTo}");
print_r($result->result_array());

Method Two: 方法二:

$result = $this->db->query("EXEC SendPost_All_Periodic @DateAs='$DateAs', @DateTo='$DateTo'");
print_r($result->result_array());

Hopefully this helps. 希望这会有所帮助。 If so, let me know which one works. 如果是这样,请告诉我哪个可行。 If not, see if this Stackoverflow article points you in the right direction: How to call a stored procedure in CodeIgniter? 如果不是,请查看这篇Stackoverflow文章是否为您指明了正确的方向: 如何在CodeIgniter中调用存储过程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM