Use distinct in Row

Asked

Viewed 62 times

0

Good staff I am with a doubt, I am trying to return from the comic only states that possess the searched term however I do not want to repeat them. follows 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>
  • Good afternoon. From what I see your query is correct. It is giving some error or similar?

  • 1

    But I guess your problem isn’t with the distinct. He prints the '--'? From what I’m seeing, the error is in Else, I think you’d have to loop and print the results and not the way you’re doing it there. I just don’t answer because I’m not smart in PHP.

  • Looks like we’re missing one mysql_fetch_assoc() before the echo of option. $estado was defined where?

  • at the beginning of the code after the connection

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>
  • It really was that, I was going to see for help

Browser other questions tagged

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