Argument 2 passed to sqlsrv_connect() must be of the type array, string Given

Asked

Viewed 1,200 times

0

I searched many places and was amazed to find almost nothing of PHP OO using SQL Server. In short, I cannot understand what is wrong here, I think it helps, I thank you.

<?php

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

    function __construct() {
        $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->Con, $this->Database);

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

}

?>

This is my Connection class following my Administrator class

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

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

    public function inserir($Nome, $Endereco, $Telefone){
        $sql = "INSERT INTO CADPES (NOME, ENDERECO, NUMERO)VALUES($this->Nome, $this->Endereco, $this->Telefone)";
        $query = sqlsrv_query($this->Con->Conectar(), $sql);
    }

}

I wish I knew where I was going wrong !

  • Puts the full error message in the question.

  • 1

    missing a return after that line $this->Coninfo = sqlsrv_connect($this->Con, $this->Database); ie, Return $this->Coninfo; if everything is all right miss this. Also has design problems plus this is another story

  • 1

    Did not resolve =( this is the Catchable fatal error: Argument 2 passed to sqlsrv_connect() must be of the type array, string Given, called in

1 answer

0


The passage of the arguments is exchanged on sqlsrv_connect() the signature of the function is

sqlsrv_connect ( string $servername [, array $connectionInfo ] )

Change:

$this->Coninfo = sqlsrv_connect($this->Con, $this->Database);

To:

$this->Coninfo = sqlsrv_connect($this->Database, $this->Con);
  • yes, it really helped this exchange vlw, but now I have error in the query I even changed to sqlsrv_query($this->Con->Connect(), $sql) but it returns me that sqlsrv_query() expects Parameter 1 to be Resource, Boolean Given in

  • @Alisonpaulo sqlsrv_query() it is necessary to inform 3 arguments, in that order, connection, string sql and values. Change your code to: $sql = "INSERT INTO CADPES (NOME, ENDERECO, NUMERO)VALUES(?,?,?)";&#xA;$params = array($this->Nome, $this->Endereco, $this->Telefone);&#xA;$query = sqlsrv_query($this->Con->Conectar(), $sql, $params);

  • truth, I saw in php.net the syntax, I did what you said but is still returning the same error no longer know what I do =( but still vlw

  • @Alisonpaulo see if an error like this appears, sqlsrv_query($this->Con->Conectar(), $sql, $params) or die(print_r(sqlsrv_errors()))

  • had already done this behind the same thing An invalid Parameter was passed to sqlsrv_query

Browser other questions tagged

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