Do one or more searches in the database with php

Asked

Viewed 37 times

0

I’m having trouble searching my database with the PHP code where I can search for an "item" but with more than one keyword.

For example: I have a database in which I shelter key information such as ip’s, user names etc. I was able to make a search code but only if I search with the desired IP, as I would also search by name and other information.

Follows 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?

  • 1

    In the $search variable, the user is typing only by IP or has more parameters?

  • You want to recover bank records according to your $buscar and this can be an IP, Name or other information ? That ? If so, tried to use a OR in your select ?

2 answers

1


You can concatenate several LIKE or any other checks with AND or other logical mysql operators.

For example:

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

In this case he would be returning all lines that meet the 3 proposed conditions.

In case you check if any of the columns satisfies the word typed by the user, you can use the OR thus:

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

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

Take a look at the logical mysql operators you might be using: https://dev.mysql.com/doc/refman/8.0/en/logical-operators.html

  • Vinicius if what he says in the question makes sense , using the example of "one item" for several keywords , the ideal would be to concatenate Likes with the operator "OR". When understanding the question he has a single search filter where , OR he would type the name or the ip and so on.

  • @Felipe then, I may have misinterpreted, but from what I understood he would like to pull lines that satisfy more than one situation, I took into consideration his phrase " also by name and other information." I could be wrong.

  • 1

    @Felipe I think it’s now more complete!

  • 1

    It worked out here! I was not very clear on the question but it was this idea that they understood. Thank you guys very much!

1

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

In this case, you can use a OR in your consultation:

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 several OR.

I hope I’ve helped.

  • Thank you very much! It worked out and that’s exactly what I was thinking of doing!

Browser other questions tagged

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