Use distinct in row

0

Well, I'm having a question, I'm trying to return from bd only the states that have the term searched for, but I do not want any repeats. follow the code

<?php
$search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING);
$q = "SELECT DISTINCT estado FROM classificados WHERE texto LIKE '%".$search_term."%'  ";
$r = mysql_query($q);
if(mysql_num_rows($r)==0)// se nao encontrar resultado
{
echo "--";
}
else //se encontrar algum resultado
{ echo " <option value='index.php?s=$search_term&e=$estado'> $estado </option>" ;} ?>
</select>
    
asked by anonymous 22.08.2014 / 21:43

1 answer

2

As pointed out by Edgar, you need to iterate the records returned for this use the function mysql_fetch_assoc()

}else{
   while($linha = mysql_fetch_assoc($r){
      echo "<option value='index.php?s=$search_term&e=$linha['estado']>
               $linha['estado']
            </option>";
   }
}
?>
</select>
    
22.08.2014 / 22:35