Error Insert PHP OO Sql Server sqlsrv_query () expects parameter 1 to be resource, boolean given in

1

This class is giving me a confessing job to you that I do not see error here, I already saw in php.net I think I'm following all the logic and syntax. If anyone can help me solve this problem, I am very grateful.

<?php
include_once 'Conexao.class.php';
class Administrador {
    private $Nome;
    private $Endereco;
    private $Telefone;
    private $Conn;

    public function __construct() {
        $this->Conn = new Conexao();
    }

    public function inserir($Nome, $Endereco, $Telefone){
        $sql = "INSERT INTO CADPES (NOME, ENDERECO, NUMERO)VALUES(?,?,?)";
        $params = array($this->Nome, $this->Endereco, $this->Telefone); 
        $query = sqlsrv_query($this->Conn->Conectar(), $sql, $params) or die( print_r( sqlsrv_errors(), true));
    }

}

class adm and now my class Connection

<?php

class Conexao{
    private $Localhost  = 'NOTEBOOK101010\SQLEXPRESS';
    private $User  = 'sa';
    private $Pass  = '';
    private $Database  = 'ALISON';
    private $Con = null;
    private $Coninfo = null;

    function __construct() {
        //return $this->Coninfo;
    }

    public function Conectar(){
        $this->Localhost;
        $this->User;
        $this->Pass;
        $this->Database;

        $this->Con = array("Database" => $this->Localhost, "UID" => $this->User, "PWD" => $this->Pass);
        $this->Coninfo = sqlsrv_connect($this->Database, $this->Con);
        return $this->Coninfo;

        if($this->Coninfo){
            echo "Conectou";
        }else{
            die(print_r(sqlsrv_error(), true));
        }
    }
}

?>
    
asked by anonymous 05.10.2016 / 23:47

2 answers

1

Connecting () is probably not able to connect and returns a false. Return from sqlsrv_connect:

A connection resource. If a connection cannot be successfully opened, FALSE is returned. 

And detail:

return $this->Coninfo;

    if($this->Coninfo){
        echo "Conectou";
    }else{
        die(print_r(sqlsrv_error(), true));
    }

This code tread does not make sense since you're checking after the return. Soon it will never arrive in if.

    
05.10.2016 / 23:51
1
$this->Con = array("Database" => $this->Database, "UID" => $this->User, "PWD" => $this->Pass);
$this->Coninfo = sqlsrv_connect($this->Localhost, $this->Con);

Damn this was the error I was giving, I changed the Database by Localhost I confused at the time I gave var_dump and vi.

    
06.10.2016 / 00:14