php - sqlsrv_query()期望参数1是资源,给定布尔值

时间:2014-11-14 02:48:12

标签: php sql-server

我试图通过sqlsrv_query运行一个简单的查询。我有一个连接到数据库的类:

class ConexionDB
{

    private $server = SERVIDOR;
    private $user = USUARIO;
    private $passw = PASSW;
    private $db_name = DB_NAME;

    private $conn;
    private $error;
    private $stmt;

    function __construct()
    {
        try {

            $this->conn = sqlsrv_connect($this->server, 
                            array(
                                "Database" => $this->db_name,
                                "UID" => $this->user,
                                "PWD" => $this->passw
                            )
                    );

        } catch (Exception $e) {
            throw $e;
        }
    }

    function query($query)
    {
        $this->stmt = sqlsrv_query($this->conn, $query);
    }

}

我有一个DAO类,可以像这样进行查询:

$database = new ConexionDB();

$sql = " SELECT *
            FROM SistemaWeb.dbo.Empresa  ";

$database->query($sql);

但结果给了我这个错误:

sqlsrv_query() expects parameter 1 to be resource, boolean given

可能有什么不对?谢谢你的回复。

编辑:以下是通过SQL Management进行查询的结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

Hei朋友尝试更改上面的字段 从

private $server = SERVIDOR;
private $user = USUARIO;
private $passw = PASSW;
private $db_name = DB_NAME;

private $server = "SERVIDOR";
private $user = "USUARIO";
private $passw = "PASSW";
private $db_name = "DB_NAME";

这应该避免连接失败。