Hello.

I have lines in the database, some have the number 2, how can I print all the lines except the one with this number. I tried NOT like '% 2%', it does not work ... Or maybe you can just exclude this figure in the PHP script when outputting? Help please, I’m already exhausted, I’m suffering for 2 hours, trying to get something out without this figure.

$avs = mysql_query("select * from fr where idus='1'"); while ($av = mysql_fetch_array($avs)) { $trs = mysql_query("select * from us where id='$av[id]'"); while ($tr = mysql_fetch_array($trs) { echo $tr[id]; } } 

Here from us it is necessary to display all id, except id 2

  • Query text complete - Sergey
  • See, edited - Sasha Osipov
  • What id ? char , Int ? if int , then not cast(id as char) like '%2%' to char : not cast(id as char) like '%2%' - Sergey
  • And you can completely script, I just inserted immediately after '$ av [id]' and does not work - Sasha Osipov
  • Is that so? select * from us where id='$av[id]' not like '%2%' But this is an obvious nonsense. The script is completely possible for a fee - Sergey

2 answers 2

Does it give with 2-kami?

 select * from us where id='$av[id]' and id not like '%2%' 
  • Thank you, but you don’t know how to display one line by several parameters, that is, WHERE id = '$ av [id]' AND id = '$ av [idus'' - Sasha Osipov
  • It is probably necessary to replace AND with OR. - msi
  • Similarly, not checked, stepped something. - Sasha Osipov

It can do what you want with one request, nested loops on php are much slower than a single request.

 select * from fr join us on us.id=fr.id where fr.idus='1' and fr.id!=2 

Excludes entries with id = 2. If it is necessary to exclude it is containing a deuce, as you wrote in the question (ie, 12 and 25), then correct the condition to not like.

If it still seems to you that the nested loop is necessary (although most likely it seems to you wrong) you can do the necessary checking at the beginning of the loop before executing the query and not perform a subquery for the unnecessary data at all:

 while ($av = mysql_fetch_array($avs)) { if($av[id]==2) continue; $trs = mysql_query("select * from us where id='$av[id]'"); ... }