Do one or more searches on the database with php

0

I'm having a hard time searching my database with PHP code where I can search for an "item" but with more than one keyword.

For example: I have a bank that shelters main information such as ip's, username, etc. I was able to do a search code but only if I search with the desired IP, as I would to search by name and other information as well.

Follow my code:

<?php
    $buscar = $_POST['pesquisar'];
    $sql = mysqli_query($conn, "SELECT * FROM principal WHERE ip LIKE '%".$buscar."%' ");
    $row = mysqli_num_rows($sql);

    if($row > 0){
        while($linha = mysqli_fetch_array($sql)){
            $ip = $linha['ip'];
            $nome = $linha['nome'];
            $setor = $linha['setor'];
            $idpc = $linha['idpc'];
            $tag = $linha['tag'];
            $modelo = $linha['modelo'];
            $tipo = $linha['tipo'];
            $so = $linha['so'];
            $observacao = $linha['observacao'];
            echo "<strong>IP: </strong>".@$ip;
            echo "<br /><br />";
            echo "<strong>NOME: </strong>".@$nome;
            echo "<br /><br />";
            echo "<strong>SETOR: </strong>".@$setor;
            echo "<br /><br />";
            echo "<strong>IDPC: </strong>".@$idpc;
            echo "<br /><br />";
            echo "<strong>TAG: </strong>".@$tag;
            echo "<br /><br />";
            echo "<strong>MODELO: </strong>".@$modelo;
            echo "<br /><br />";
            echo "<strong>TIPO: </strong>".@$tipo;
            echo "<br /><br />";
            echo "<strong>SO: </strong>".@$so;
            echo "<br /><br />";
            echo "<strong>OBSERVAÇÃO: </strong>".@$observacao;
            echo "<br /><br />";
        }
    }else{
        echo "Não há nenhum usuário registrado!";
    }
?>

Could you help me?

    
asked by anonymous 12.12.2018 / 13:55

2 answers

1

You can concatenate multiple LIKE or any other checks with AND or other mySQL logical operators.

For example:

SELECT * FROM principal WHERE ip LIKE '%".$buscar."%' AND nome LIKE 'nome' AND setor LIKE 'setor'

In this case it would be returning all rows that satisfy the 3 conditions proposed.

In case you check if any of the columns satisfies the word entered by the user, you can use OR like this:

SELECT * FROM principal WHERE ip LIKE '%".$buscar."%' OR nome LIKE 'nome' OR setor LIKE 'setor'

In the above query it would be returning all rows that satisfy at least one of the proposed conditions.

Take a look at the mySQL logical operators you might be using: link

    
12.12.2018 / 14:02
1

If I understand it straight you will only have the variable $buscar as a filter, it could be an IP, Name or other information.

In this case, you can use OR in your query:

SELECT * FROM principal 
    WHERE ip LIKE '%".$buscar."%' 
    OR nome LIKE '%".$buscar."%' 
    OR setor LIKE '%".$buscar."%'
    //OR .. OUTROS FILTROS ..

Remembering that this may bring other information that does not match your search due to the use of multiple OR .

I hope I have helped.

    
12.12.2018 / 14:10