How to search a mysql table with two filters?

Asked

Viewed 35 times

0

I have a page where it returns a table from my database, but as this table receives several data I am trying to set up a filter for it, I made a form and from it a filter, follows example:

$busca1 = $_POST['input001'];

if ($busca1 != "") 
{
    $sqli = "SELECT * FROM table WHERE column = '$busca1'";
    $resultado = mysqli_query($conn,$sqli) or die("Erro ao retornar dados");

    while ($registro = mysqli_fetch_array($resultado))
    {   
        echo "<tr><td>".$registro["Coluna1"] . "</td><td>". $registro["Coluna2"] . "</td><td>". $registro["Coluna3"] . "</td><td>". $registro["Colun4"] . "</td><td>". $registro["Coluna5"] . "</td><td>". $registro["Coluna6"] . "</td><td>". $registro["Coluna7"] . "</td><td>". $registro["Coluna8"] . "</td><td>". $registro["Coluna9"] . "</td><td>". $registro["Coluna10"] . "</td><td>". $registro["Coluna11"]. "</td><td>". $registro["Coluna12"] ."</td></tr>";
    }
}

Now I wanted to know how to search joining two fields, it would be something like if ($busca1 != "" && busca2 != "") and then repeat for all possibilities or there is a better way and with less repetition of code?

  • 1

    And Nathan resolved?

1 answer

1


Simple search

 caso um condição dependa da outra
    $sqli = "SELECT * FROM table WHERE column = '$busca1' AND column3 = '$busca3' ";
 caso seja idependentes uma da outra
    $sqli = "SELECT * FROM table WHERE column = '$busca1' OR column3 = '$busca3' ";

Advanced search a little faster

$sqli = "SELECT * FROM table WHERE column IN (1,2,3,4) ";

array,

$arr = [1,2,3,4];
$arr = implode(',', array_map('intval', $arr));
$sqli = "SELECT * FROM table WHERE column IN (1,2,3,4) ";

Browser other questions tagged

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