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

时间:2022-10-06 19:50:01

I am using SQL server2008 as database and I have written stored procedure in MSSQL Server 2008. It's working fine in MSSQL Server 2008. I want to call this Stored Procedure from codeigniter. For that I wrote the code like this :

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

phpService.php:

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:

apimodel.php:

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

    return $query->result();

}

when I Execute Procedure without parameter it Working Fine

当我执行没有参数的程序时,它运行良好。

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

        return $query->result();

    }

But, When I Execute Procedure with parameter it's not working

但是,当我用参数执行程序时,它就不起作用了。

Can anyone tell me What am I missing here ?

有人能告诉我我在这里漏掉了什么吗?

Thanks in advance

谢谢提前

2 个解决方案

#1


1  

First of all, if you are using Codeigniter, I recommend use their database class to connect to MSSQL Server. You can read more on this here: http://ellislab.com/codeigniter/user-guide/database/configuration.html

首先,如果您正在使用Codeigniter,我建议使用它们的数据库类连接到MSSQL服务器。您可以在这里阅读更多内容:http://ellislab.com/codeigniter/userguide/database/configures.html。

If you aren't auto-connecting to your database you can connect like this: $this->load->database('default');

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

Once you have your configuration setup, you can have a function like this in your model:

一旦你有了配置设置,你就可以在你的模型中有这样的功能:

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

#2


0  

oh..............after long RND I got ans

哦..............经过长时间的追踪,我得了ans

PhpService.php:

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:

apimodel.php:

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

        return $query->result();
    }

#1


1  

First of all, if you are using Codeigniter, I recommend use their database class to connect to MSSQL Server. You can read more on this here: http://ellislab.com/codeigniter/user-guide/database/configuration.html

首先,如果您正在使用Codeigniter,我建议使用它们的数据库类连接到MSSQL服务器。您可以在这里阅读更多内容:http://ellislab.com/codeigniter/userguide/database/configures.html。

If you aren't auto-connecting to your database you can connect like this: $this->load->database('default');

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

Once you have your configuration setup, you can have a function like this in your model:

一旦你有了配置设置,你就可以在你的模型中有这样的功能:

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

#2


0  

oh..............after long RND I got ans

哦..............经过长时间的追踪,我得了ans

PhpService.php:

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:

apimodel.php:

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

        return $query->result();
    }