简体   繁体   English

将执行存储过程从PHP发送到Microsoft SQL SERVER

[英]Issue executing stored procedure from PHP to a Microsoft SQL SERVER

I am using the code igniter framework. 我正在使用代码点火器框架。 And I been trying to execute a stored procedure(no params) I wrote on Microsoft SQL Server 2008 from PHP but i get an error . 我一直在尝试执行我在PHP上从Microsoft SQL Server 2008上编写的存储过程(没有参数),但是我收到了一个错误。 This stored procedure runs good on Microsoft SQL Server with no errors. 此存储过程在Microsoft SQL Server上运行良好,没有错误。 I am using sqlsrv as the driver and I have a PHP version 5.2 if that helps. 我使用sqlsrv作为驱动程序,如果有帮助,我有一个PHP版本5.2。

This is the error I get 这是我得到的错误

Error Number: 01000
[Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor
Exec sp_test

The following is part of the code I have 以下是我所拥有的代码的一部分

function index(){
  $this->load->database();
  $query=$this->db->query("Exec sp_test");
  var_dump($query->result());
}

I replace the actual query with an actual query and it does work but not with a sp call. 我用实际查询替换实际查询,它确实有效,但没有sp调用。 Any help will be appreciated, I tried for so long on this and i can't get it to work. 任何帮助将不胜感激,我在这方面试了这么久,我无法让它工作。 Thanks 谢谢

I had faced the same issue. 我遇到过同样的问题。 Removing the 'EXEC' worked for me. 删除'EXEC'对我有用。

$query = $this->db->query("procedure_name parameter_1, parameter_2"); $ query = $ this-> db-> query(“procedure_name parameter_1,parameter_2”);

I've been stumbling with a similar error for some time. 一段时间以来,我一直在遇到类似的错误。 The thing is that the execution of the stored rpocedure returned a state code 01000/0, which is not an error but a warning, and still don't know why it's beeing returned. 问题是存储的rpocedure的执行返回了状态代码01000/0,这不是错误而是警告,但仍然不知道它为什么要返回。

Anyway, the thing is that the SP was a simple select, and whenever I runned it with the query tool, the select worked just fine, even invoking this select from php with codeigniter the values where returned correctly, but when I tried to use the stored procedure, it keep failing. 无论如何,事情是SP是一个简单的选择,每当我用查询工具运行它时,选择工作得很好,甚至调用这个选择从php与codeigniter正确返回的值,但当我尝试使用存储过程,它一直在失败。

Finally got it working and the solution was to simply mark to not return warnings as errors. 最后让它工作,解决方案是简单地标记为不将警告作为错误返回。 Here's a sample code: 这是一个示例代码:

 $sp = "MOBILE_COM_SP_GetCallXXX ?,?,?,?,?,?,?,?,?,? "; //No exec or call needed

 //No @ needed.  Codeigniter gets it right either way
 $params = array(
            'PARAM_1' => NULL,
            'PARAM_2' => NULL,
            'PARAM_3' => NULL,
            'PARAM_4' => NULL,
            'PARAM_5' => NULL,
            'PARAM_6' => NULL,
            'PARAM_7' => NULL,
            'PARAM_8' => NULL,
            'PARAM_9' => NULL,
            'PARAM_10' =>NULL);

 //Here's the magic...
 sqlsrv_configure('WarningsReturnAsErrors', 0);

 //Even if I don't make the connect explicitly, I can configure sqlsrv
 //and get it running using $this->db->query....

 $result = $this->db->query($sp,$params);

That's it. 而已。 Hope it helped 希望它有所帮助

I also had the similar issue while connecting to MS SQL Server 2008 using sqlsrv driver. 使用sqlsrv驱动程序连接到MS SQL Server 2008时,我也遇到了类似的问题。 My issue was resolved using the following code: 我的问题使用以下代码解决:

$result = $this->db->query("PROCEDURE_NAME {$param_1}, {$param_2}")
                   ->result_array();

I have this helps someone out there =] 我有这个帮助那里的人=]

I am facing fetch data from sql server stored procedure in codeignitor but finally resolved it. 我面临从codeignitor中的sql server存储过程中获取数据,但最终解决了它。

    $sync= $this->load->database('sync', TRUE);
    $query = $sync->query('PROCEDURE_NAME')->result_array();
    echo '<pre>';
    print_r($query);

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

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