Error Insert PHP OO Sql Server sqlsrv_query() expects Parameter 1 to be Resource, Boolean Given in

Asked

Viewed 780 times

1

Guys this Class is giving me a work I confess to you that I see no error here, I have seen in php.net I believe that I am following all the logics 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));
    }

}

Adm class and now my Connected class

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

?>

2 answers

1


Probably Connect() is unable to connect and returns a false. Return of 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 chunk of code makes no sense as you are checking after Return. Soon it will never get to if.

And it is also good to treat the return before using it directly in the method because if it returns something else if not a Source it will give error, and we do not want error npé?

  • I changed the Return as you said I put it in the condition if now it brings another error +/ vc had already given the hint. But how do I fix it? sqlsrv_query() expects Parameter 1 to be Resource, null Given in

  • In fact you have to put something like "echo $this->Coninfo;" then a var_dump(sqlsrv_errors();die; to see what is returning in Conifo and if there was an error. From there you see what needs to change for your connection to work

1

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

Damn this was a mistake I was making, I switched the Database for the Localhost confounding at the time of var_dump is vi.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.