Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Sqlsrv驱动程序从存储过程获取数据_Php_Sql Server 2008_Codeigniter_Rest_Stored Procedures - Fatal编程技术网

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

Php 使用Sqlsrv驱动程序从存储过程获取数据,php,sql-server-2008,codeigniter,rest,stored-procedures,Php,Sql Server 2008,Codeigniter,Rest,Stored Procedures,我正在使用sqlserver2008作为数据库,并且我已经在mssqlserver2008中编写了存储过程。它在MSSQL Server 2008中运行良好。我想从codeigniter调用此存储过程。为此,我编写了如下代码: phpService.php: public function Login($username, $password) { $this->load->model('Apimodel'); $result = $this->Apimodel

我正在使用
sqlserver2008
作为数据库,并且我已经在
mssqlserver2008
中编写了存储过程。它在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));
}
function Login($UserName,$Password)
{               
    $this->db = $this->GetDB();
    $query =  $this->db->query("EXEC Login");

    return $query->result();

}
    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));

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

        return $query->result();
    }
apimodel.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));
}
function Login($UserName,$Password)
{               
    $this->db = $this->GetDB();
    $query =  $this->db->query("EXEC Login");

    return $query->result();

}
    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));

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

        return $query->result();
    }
当我在没有参数的情况下执行过程时,它工作正常

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

        return $query->result();

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

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

        return $query->result();

    }
谁能告诉我我在这里错过了什么


首先,如果您使用的是Codeigniter,我建议您使用他们的数据库类连接到MSSQL服务器。您可以在此处阅读更多信息:

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

配置设置完成后,您可以在模型中实现如下功能:

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

哦………很久以后,我得了ans

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));
}
function Login($UserName,$Password)
{               
    $this->db = $this->GetDB();
    $query =  $this->db->query("EXEC Login");

    return $query->result();

}
    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));

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

        return $query->result();
    }
apimodel.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));
}
function Login($UserName,$Password)
{               
    $this->db = $this->GetDB();
    $query =  $this->db->query("EXEC Login");

    return $query->result();

}
    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));

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

        return $query->result();
    }

这可能会对@Deepanshu有所帮助:我不是从这个链接得到的。你能解释一下吗?你试过使用绑定参数吗<代码>$this->db->query(“Exec Login?,?”,数组($UserName,$Password))如果密码中有引号字符,将防止出现错误。是否尝试了$this->db->query(“EXEC Login('$UserName','$password'));他他哥们我尝试了链接alreadyFatal错误:在第87行对\apimodel.php中的非对象调用成员函数result()。$query=$this->db->query(“EXEC Common_Login($username,$password)”);听起来您仍然没有使用CI的数据库类。确保你仔细阅读了它和模型:和。并确保将字符串括在单引号中。当我在执行过程中没有参数时,它将正常工作,但有参数时它不工作。是否在参数周围加引号?获取整个查询字符串并在MSSQL Server中运行,以查看是否有任何错误:echo“EXEC Common_Login(“$username”,“$password”)”我经常使用
$this->db->query()
来运行存储过程。现在运行查询时会发生什么?你有错误吗?如果检查
$this->db->last\u query()
,查询是否正确?