Error: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given

Asked

Viewed 939 times

0

In my project I put a search bar, in which the user informs a value to be searched and selects the column in which he wants to search. What will be searched comes from an input type="text" field and the input type="radio" column, both in the same form. The problem is that when I enter the variables with the values brought from the form, php returns an error:

mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in C: xampp htdocs CRUD php listar_client.php on line 178

  <?php
  //Início da listagem

  $coluna = $_POST['coluna'];
  $pesquisa = $_POST['pesquisa'];

  $servidor = 'localhost';
  $usuario = 'root';
  $senha = '';
  $banco = 'portfolio';

if( $pesquisa == "")
{   
    if( ($coluna == "") or ($coluna == "tudo"))
    {
        $COMANDO_SQL = "SELECT * FROM cliente ";        
    }
}
else
{
    $COMANDO_SQL = "SELECT * FROM cliente WHERE '".$coluna."' LIKE '%'".$pesquisa."'%' ";   
}

$link = mysqli_connect($servidor,$usuario,$senha,$banco);
$resultado_listagem = mysqli_query($link,$COMANDO_SQL);  
?>

Php for the listing:

<?php
        while( $linha = mysqli_fetch_assoc($resultado_listagem) )
        {   
    ?>
            <tr>
                <td data-title="CPF/CNPJ"><?php echo $linha['CPF_CNPJ']; ?></td>
                <td data-title="Nome/Razão Social"><?php echo $linha['NOME_COMPLETO_RAZAO_SOCIAL']; ?></td>
                <td data-title="Nome Fantasia"><?php echo $linha['NOME_FANTASIA']; ?></td>
                <td data-title="Telefone"><?php echo $linha['TELEFONE'];     ?></td>
                <td data-title="Rua"><?php echo $linha['RUA']; ?></td>
                <td data-title="Bairro"><?php echo $linha['BAIRRO']; ?></td>
                <td data-title="Cidade"><?php echo $linha['CIDADE'];; ?></td>
                <td data-title="Estado"><?php echo $linha['ESTADO']; ?></td>
                <td data-title="Ações">
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"
                        data-cpf="<?php echo $linha['CPF_CNPJ']; ?>"
                        data-nome="<?php echo $linha['NOME_COMPLETO_RAZAO_SOCIAL']; ?>"
                        data-nomefantasia="<?php echo $linha['NOME_FANTASIA']; ?>"
                        data-telefone="<?php echo $linha['TELEFONE']; ?>" 
                        data-rua="<?php echo $linha['RUA']; ?>" 
                        data-bairro="<?php echo $linha['BAIRRO']; ?>"
                        data-cidade="<?php echo $linha['CIDADE']; ?>"
                        data-estado="<?php echo $linha['ESTADO']; ?>" >
                        Editar
                    </button>
                </td>
                <td data-title=""><button type="button" class="btn btn-danger">Excluir</td>
            </tr>
    <?php
        }
    ?>

HTML form code:

<!-- BARRA DE PESQUISA -->
    <div class="row">
        <div class="col-md-12">
            <form action="listar_cliente.php" method="post">
                <div class="col-md-12">
                    <input type="text" class="form-control" name="pesquisa" id="pesquisa" placeholder="Pesquisar por..." >
                    <button class="btn " type="submit">
                            <i class="glyphicon glyphicon-search"></i>
                    </button>
                </div>  
                <br>
                <input type="radio" name="coluna" id="coluna" value="tudo" > Todos os dados 
                <input type="radio" name="coluna" id="coluna" value="CPF_CNPJ" > CPF/CNPJ   
                <input type="radio" name="coluna" id="coluna" value="NOME_COMPLETO_RAZAO_SOCIAL"> Nome
                <input type="radio" name="coluna" id="coluna" value="NOME_FANTASIA"> Nome Fantasia
                <input type="radio" name="coluna" id="coluna" value="TELEFONE"> Telefone
                <input type="radio" name="coluna" id="coluna" value="RUA"> Rua
                <input type="radio" name="coluna" id="coluna" value="BAIRRO"> Bairro
                <input type="radio" name="coluna" id="coluna" value="CIDADE"> Cidade
                <input type="radio" name="coluna" id="coluna" value="ESTADO"> Estado
            </form>
        </div>
    </div>
</div>
  • What error does it return to you? Paste the entire message.

  • mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in C: xampp htdocs CRUD php listar_client.php on line 178

2 answers

1

So from what I can tell you’re doing it wrong try it here. I use practically the same thing in one of my applications, like the comando that you are passing to the bank is already inside the php does not have why concatenate just you call the variable normally. Use the following command 'sql':

$COMANDO_SQL = "SELECT * FROM client WHERE '$coluna' LIKE '%$search%' ";

  • Keeps not returning the same error " mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in C: xampp htdocs CRUD php listar_client.php on line 178"

1


First, columns cannot be enclosed in single quotes:

... WHERE '".$coluna."'

Second in doing this:

LIKE '%'".$pesquisa."'%'

It’s probably generating something like:

LIKE '%'exemplo'%'

In other words, the search is not within the LIKE, it has extra quotes.

The correct would probably be something like:

"SELECT * FROM cliente WHERE ".$coluna." LIKE '%".$pesquisa."%'"

Now most importantly, always check mysqli_query, you can use die, for example:

else
{
    $COMANDO_SQL = "SELECT * FROM cliente WHERE ".$coluna." LIKE '%".$pesquisa."%'";   
}

$link = mysqli_connect($servidor,$usuario,$senha,$banco);
$resultado_listagem = mysqli_query($link, $COMANDO_SQL) or die(mysqli_error($link));

So avoid entering while chance occurs any failure.

  • The problems were in the Apas. I thought the syntax was correct, because I always used this form ' ". $variavel." ' to concatenate variables in sql command.

  • So much so that in the function I use to save data, the sql command has simple quotes in the column $COMANDO_SQL_CLIENTE = "INSERT INTO client ( CPF_CNPJ, TELEPHONE, NOME_COMPLETO_RAZAO_SOCIAL, NOME_FANTASIA, CITY, STREET, STATE, NEIGHBORHOOD) values( '". $CPF_CNPJ."', '". $PHONE."', '". $NOME_COMPLETO_RAZAO_SOCIAL."', '". $NOME_FANTASIA."', '". $CITY."', '". $STREET."', '". $STATE."', '". $NEIGHBORHOOD."')";

  • @Igor no, that’s not the columns, that’s the "values" of the columns, the columns are the part that this without quotes: CPF_CNPJ, TELEFONE, NOME_COMPLETO_RAZAO_SOCIAL, NOME_FANTASIA, CIDADE, RUA, ESTADO, BAIRRO. Summarizing: Values use quotes, columns do not use ;)

  • 2

    In fact, I made a huge mistake. Grateful for everything guy<Joinha here>.

Browser other questions tagged

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