使用Sqlsrv驱动程序从存储过程中获取数据

时间:2013-09-02 06:49:43

标签: php sql-server-2008 codeigniter rest stored-procedures

我使用SQL server2008作为数据库,我在MSSQL Server 2008编写了存储过程。它在MSSQL Server 2008中运行良好。我想从codeigniter调用此存储过程。为此我写了这样的代码:

phpService.php:

public function Login($username, $password)
{
    $this->load->model('Apimodel');
    $result = $this->Apimodel->Login($username,$password);

    header('Content-type: application/json');
    echo json_encode(array('LoginResponce'=>$result));
}

apimodel.php:

function Login($UserName,$Password)
{               
    $this->db = $this->GetDB();
    $query =  $this->db->query("EXEC Login");

    return $query->result();

}

当我执行没有参数的程序时,工作正常

function Login($UserName,$Password)
    {               
        $this->db = $this->GetDB();
        $query =  $this->db->query("EXEC Login '$UserName','$Password'");

        return $query->result();

    }

但是,当我使用参数执行过程时它不起作用

谁能告诉我,我在这里失踪了什么?

提前致谢

2 个答案:

答案 0 :(得分:1)

首先,如果您使用Codeigniter,我建议使用他们的数据库类连接到MSSQL Server。您可以在此处详细了解:http://ellislab.com/codeigniter/user-guide/database/configuration.html

如果您没有自动连接到数据库,可以这样连接:$this->load->database('default');

完成配置设置后,您可以在模型中使用这样的功能:

function login($username, $password) {
    return $this->db->query("EXEC spLogin '$username', '$password'")->result();
}

答案 1 :(得分:0)

哦..............经过长时间的RND我得到了答案

<强> PhpService.php:

    public function Login($username, $password)
    {

        $this->load->model('Apimodel');
        $result = $this->Apimodel->Login($username,$password);

        header('Content-type: application/json');
        echo json_encode(array('LoginResponce'=>$result));

    }

<强> apimodel.php:

function Login($username, $password)
    {
        $this->db = $this->GetDB();
        $query =  $this->db->query("EXEC Login $username,$password");

        return $query->result();
    }