HOLA DEBES RESOLVERLO DE LA SIGUIENTE FORMA, PARA TENER MAYOR CERTEZA DE QUE SE ENCONTRARA CORRECTA TU CONSULTA.
Código PHP:
Ver originalif(!empty($sexo)){$filtros[]="A2='$sexo' ";}//ingresas tu filtro a un array solo si tiene valor if(!empty($comunidad)){$filtros[]="A8='$comunidad' ";}//ingresas tu filtro a un array solo si tiene valor if(!empty($ocupacion)){$filtros[]="A7='$ocupacion' ";}//ingresas tu filtro a un array solo si tiene valor
$i=0;
$num=count($filtros);//cuentas cuantos filtros tienes $and=" AND ";
foreach($filtros as $filtro){//recorres tu array
$i++;
$opciones.=$filtro;//cada elemento del array lo colocas como string
if($i<$num){$opciones.=$and;}//agregas AND cuando tienes + de una condicional pero no en la ultima<--MUY IMPORTANTE
}
$where="";
if(!empty($opciones)){$where=" WHERE ";}//asignas WHERE solo si te han enviado filtros, en caso contrario seleccionaras toda la base
DEBES REVISAR QUE LOS FILTROS TE LLEGUEN CORRECTAMENTE
var_dump($comunidad);//esto te mostrara que tiene la variable.
Si no contiene nada debes revisar el name="xxx" y el value="yyyy" de los campos del formulario.
Saludos