Warning: sqlsrv_query() expects Parameter 1 to be Resource, Object Given

Asked

Viewed 133 times

1

I’m having trouble in a Cód. which has the function of returning a keyword search of a bank record.

Error: Warning: sqlsrv_query() expects Parameter 1 to be Resource, Object Given

I tested in Mysql database, and had no problem, but the database I need to use is sqlserver.

PS: The connection to the bank is working properly. Code. connection to the bank:

<?php

// Conexão com banco SqlServer

$servidor = "localhost"; //host
$connectionInfo = array("Database"=>"dcm_digital", "UID"=>"root", "PWD"=>"root", "CharacterSet"=>"UTF-8");

//Criar conexão 
$conn = sqlsrv_connect($servidor, $connectionInfo);

if($conn){ echo("*** Banco conectado ***<br>"); } else { echo("Conexão falhou.<br>"); die( print_r( sqlsrv_errors(), true));}

Follow the Cód. below:

<?php

// Include conexão com o bd.
include 'conexaoBD/conexao.php';

$btn     = $_POST['btn'];
$p_chave = $_POST['palavra'];

if(isset($btn)){
    $busca = $p_chave;

    if($busca == null or $busca == " "){

        echo '<center><b>Digite a palavra-chave para pesquisar!</b></center>';

    } else {

        $busca_div = explode(' ',$busca);
        $quant     = count($busca_div);
        $exibe_id  = array("");

        for($i=0; $i<$quant; $i++){

            $pesquisa = $busca_div[$i]; 

            //Banco SqlServer
            $_sql = "SELECT * FROM conteudo_dcm WHERE DCM_conteudo LIKE '%$pesquisa%';";
            $sql  = sqlsrv_query($conn,$_sql);
            $quant_cmps = sqlsrv_num_rows($sql);


            /*/Banco SqlServer
            $_sql = "SELECT * FROM test.conteudo_dcm WHERE DCM_conteudo LIKE '%$pesquisa%';";
            $sql  = mysqli_query($conn, $_sql);
            $quant_cmps = mysqli_num_rows($sql);*/

            if($quant_cmps == 0){

                echo '<center><b>Nenhum resultado obtido!</b></center>';

            } else {
                //while ($linha = mysqli_fetch_array($sql)){
                while ($linha = sqlsrv_fetch_array($sql)){
                    $id       = $linha['DCM_id'];
                    $nome     = $linha['DCM_nome'];
                    $conteudo = $linha['DCM_conteudo'];

                    if(!array_search($id, $exibe_id)){
                        echo /*'<a href="<?php $_href; ?>">'.*/$nome/*.'</a>'*/."<br><br>".
                             substr($conteudo, 0, 600)."<b> ...</b>".
                             "<br>---------------------<br>";   

                        array_push($exibe_id,$id);
                    }
                }
            }
        }
    }
}
?>
  • First thing is already on this line $_sql = "SELECT * FROM content_dcm WHERE Dcm_content LIKE '%$search%';";, the correct is $_sql = "SELECT * FROM content_dcm WHERE Dcm_content LIKE '%$search%'"; This ; that is there can be the cause of all your problem, please see if solved. I saw that in all your querys you use the ; erroneously, remove from all and test please.

  • @Jaksonfischer even after the change, the same error persists.

  • Take a look here: link, I think this might help you.

  • 1

    The value of $conn is wrong, is receiving an object, has no way of knowing what it is, the error is inside the conexaoBD/conexao.php

  • @Guilhermenascimento I put the connection Cód. with the bank in question.

  • 1

    Okay, put before the sqlsrv_query this var_dump($conn);

  • @Guilhermenascimento I saw that in the sqlsrv_query line I had put a variable after $_sql by mistake. I removed this variable (tb removed from the question) and ran var_dump and returned the following message: Resource(6) of type (SQL Server Connection) No results obtained!

  • The guy is right, the mistake is the same sqlsrv_query() expects parameter 1 to be resource, object given, or now he’s just not "getting" into the while?

  • @Guilhermenascimento Now just don’t get into the while.

Show 4 more comments
No answers

Browser other questions tagged

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