strpos () is ending while while true

1

I loop a query. I check if the value of each record contains within any string (in the case $ message). The problem is that when the value is found, in this case inside ELSE , the while is terminated for some reason. I need the verification to continue to be done on all records, until the end of the records.

$query = "select id, id_usuario texto from palavra";
$result = mysql_query($query);
while($palavras = mysql_fetch_array($result)) {
   $valor = $palavras["texto"];
   $iduser = $palavras["id_usuario"];

   if(!strpos(strtolower($mensagem), strtolower($valor))) {
      echo " não.<BR>";
   } else {
      echo " sim.<BR>";

     $query = "select nome from usuario where id = $iduser";
     $result = mysql_query($query);
     $user_dados = mysql_fetch_row($result);

   }

}

If you shoot the internal query, the loop continues normal, as expected. But I need the consultation.

    
asked by anonymous 30.05.2016 / 21:08

3 answers

5

The problem seems to happen in the second query due to the reuse of the $result variable in the second query, rename it.

To avoid false positives with the strpos() function, use the === operator so it compares the value and type. Without it if the string is found in position zero its code will be diverted to the else block in the wrong way.

$query = "select id, id_usuario texto from palavra";
$result = mysql_query($query); //<--- primeira ocorrência

//aqui na segunda 'volta' do while $result já pode ser false ou não devolver as chaves texto e id_usuario
while($palavras = mysql_fetch_array($result)) { 
   $valor = $palavras["texto"];

   if(strpos(strtolower($mensagem), strtolower($valor)) === false) {
      echo " não.<BR>";
   } else {

      $query = "select nome from usuario where id = $iduser";
      $result = mysql_query($query); //reatribução indevida.
      $user_dados = mysql_fetch_row($result);
   }

}
    
30.05.2016 / 21:27
5

It should be an exception and the errors are off because of this you did not notice, connect PHP errors, when it is in development, in production use ini_set('display_errors', '0'); , it is probably some error in using mysql_fetch_array or in your query or something else next to it.

Another problem that may have occurred was with the $mensagem variable, make sure it exists and is in the same "scope" of the script you cited.

Do so in development, place on top of your file:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL|E_STRICT);

Another thing strpos only returns false if it does not find anything, if you do this for example:

strpos('abc', 'a');

It will return zero and as ! considers both 0 , and false as NULL as '' (empty string) then it should be used like this:

   if (strpos(strtolower($mensagem), strtolower($valor)) === false) {
        echo " não.<BR>";
   } else {
        echo " sim.<BR>";

        $query = "select nome from usuario where id = $iduser";
        $result = mysql_query($query);
        $user_dados = mysql_fetch_row($result);
   }

I noticed that you used strtolower, but this is not necessary if you use stripos :

   if (stripos($mensagem, $valor) === false) {
        echo " não.<BR>";
   } else {
        echo " sim.<BR>";

        $query = "select nome from usuario where id = $iduser";
        $result = mysql_query($query);
        $user_dados = mysql_fetch_row($result);
   }

Another important thing is to no longer use the old PHP API for mysql, prefer PDO or mysqli, read:

30.05.2016 / 21:22
0

Your problem is because our PHP friend interprets " 0 " in the same way as " false ". So when it finds the substring at the start of the String it returns 0 (which php also means false).

Change your code to:

while($palavras = mysql_fetch_array($result)) {
   $valor = $palavras["texto"];

   if(strpos(strtolower($mensagem), strtolower($valor)) === false) {
      echo " não.<BR>";
   } else {
      echo " sim.<BR>";
   }

}

That's right with 3 " === ".

    
30.05.2016 / 21:25