How to perform a mysql table search with two filters?

0

I have a page where it returns a table from my database, but as this table receives several data I am trying to mount 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 by joining two fields, would it be something like if ($busca1 != "" && busca2 != "") and then repeat for all possibilities or is there a better way with less code repetition?

    
asked by anonymous 27.09.2018 / 20:50

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) ";

with array,

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