Error: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, boolean given [duplicate]

0

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

  

mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, boolean given in C: \ xampp \ htdocs \ CRUD \ php \ customerlist.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 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>
    
asked by anonymous 24.09.2017 / 03:55

2 answers

1

First, columns can not be enclosed in single quotation marks:

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

As you do this:

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

It's probably generating something like:

LIKE '%'exemplo'%'

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

The correct one would probably look something like:

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

Now most important, 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 you avoid getting into the while some flaw happens.

    
24.09.2017 / 04:17
1

So from what I could tell you're doing it wrong try this here. I use pretty much the same thing in one of my applications, as the comando you are passing to the bank is already within php you do not have to concatenate you just call the variable normally. Use the following sql command:

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

    
24.09.2017 / 04:02