Search PHP/Mysql (show only result exactly the same as typed)

Asked

Viewed 726 times

0

php collects input data using POST method. It is connected right, is searching, but when typing the UF and Municipality it shows me not only exactly what I typed, but also the related! type typed SP - São Paulo, he does not show me only that it shows SP - São Carlos, SP- São Fulano, SP - São Siclano etc. Anyway, shows all of has s, or ã and I want to show ONLY EXACTLY WHAT I TYPED. If anyone knows and can help I am very grateful. Follow my code:

<?php

include ('conectar.php');

$connection = mysqli_connect($host, $user, $pass,$database);
 if (!$connection) { echo ("Erro ao conectar"); }

$opcao = $_POST["opcao"];
if($opcao == "2"){
    $sg_uf = '"%'. $_POST["txtsg_uf"] .'%"';
    $nm_municipio = '"%'. $_POST["txtnm_municipio"] .'%"';
    $nm_candidato = '"%'. $_POST["txtnm_candidato"] .'%"';
    $dt_ano = '"%'. $_POST["txtdt_ano"] .'%"';

$query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE ".$sg_uf." OR nm_municipio LIKE ".$nm_municipio." OR nm_candidato LIKE ".$nm_candidato." OR dt_ano LIKE ".$dt_ano."";

    $result =  mysqli_query($connection,$query);
    if (!$result) {
      die("Query inv&aacute;lida na sele&ccedil;&atilde;o dos dados");
    }

    while ($dados=  mysqli_fetch_array($result,MYSQLI_ASSOC)) {

        if ($result > 0){
            echo "Op&ccedil;&atilde;o de Busca Avan&ccedil;ada/Elei&ccedil;&otilde;es Municipais: <br />";
            echo "Encontramos!<br/ >";
            echo "UF do Estado: $dados[sg_uf]<br />"; 
            echo "Nome do Munic&iacute;pio: $dados[nm_municipio]<br />"; 
            echo "Nome do Candidato: $dados[nm_candidato]<br />"; 
            echo "Ano da Elei&ccedil;&atilde;o: $dados[dt_ano]<br />"; 
            echo "<hr>";
        }
        else {
            echo "Desculpe, nada foi encontrado.";
        }
    }
}

?>
  • use AND if you want results with all conditions. OR will return you things that have condition A, or condition B, or condition C. Something else, LIKE is not for exact searches. For exact searches the =

  • @Bacco I did so but nothing appeared $query = "SELECT * FROM resultado_votacao_candidato_zona_eleicaomunicipal "&#xA;. "WHERE sg_uf = ".$sg_uf." AND nm_municipio = ".$nm_municipio." AND nm_candidato = ".$nm_candidato." AND dt_ano = ".$dt_ano."";

2 answers

2


Well, I thank everyone who helped me, especially Bacco, thank you very much! From what I went through here I got the following result:

<?php
include ('conectar.php');


$sg_uf = '"%'. $_POST["txtsg_uf"] .'%"';
$nm_municipio = '"%'. $_POST["txtnm_municipio"] .'%"';
$nm_candidato = '"%'. $_POST["txtnm_candidato"] .'%"';
$dt_ano = '"%'. $_POST["txtdt_ano"] .'%"';

$connection = mysqli_connect($host, $user, $pass,$database);
 if (!$connection) {
 echo ("Servidor temporariamente fora de servi?o"); 
}

$query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE ".$sg_uf." and  nm_municipio LIKE ".$nm_municipio." and  nm_candidato LIKE ".$nm_candidato." and  dt_ano LIKE ".$dt_ano."";
$result =  mysqli_query($connection,$query);
if (!$result) {
  die("Query invalida");
}

while ($dados=  mysqli_fetch_array($result,MYSQLI_ASSOC)) {

if ($result > 0){

echo " Encontramos!<br />";

    echo "UF do Estado: $dados[sg_uf]<br />"; 
    echo "Nome do Municipio: $dados[nm_municipio]<br />"; 
    echo "Nome do Candidato: $dados[nm_candidato]<br />";
    echo "Ano das Eleicoes: $dados[dt_ano]<br />";  
    echo "<hr>";

}
else {
    echo "Desculpe, nada foi encontrado";
}
}
?>

That’s it’s working right there, just show me exactly what I typed.

1

Sarah, try to change your SQL to the following way:

    $query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE '%".$sg_uf."' OR nm_municipio LIKE '%".$nm_municipio." OR      nm_candidato LIKE ".$nm_candidato." OR dt_ano LIKE ".$dt_ano."";

What I did was put a simple quotation mark after the LIKE with the wildcard %.

  • Diego, done as suggested.

  • I did exactly as you put but gave invalid query I modified so: $query = "SELECT * FROM resultado_votacao_candidato_zona_eleicaomunicipal "&#xA;. "WHERE sg_uf LIKE '%".$sg_uf."' OR nm_municipio LIKE '%".$nm_municipio."' OR nm_candidato LIKE '%".$nm_candidato."' OR dt_ano LIKE '%".$dt_ano."'"; but it didn’t work, it didn’t show anything

Browser other questions tagged

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