Query does not work inside php

Asked

Viewed 51 times

2

I made a query with a Join to get information from the database, I tested it in mysql and it worked perfectly, the problem is that it does not work in php, not from the error in the execution but the return is null, even if I change the variables for text from the database it does not work, I used var_dump() to check if there was a syntax error but it does not have.

$query="SELECT t1.* FROM tb_anuncio AS t1 JOIN tb_localizacao AS t2 ON t2.cd_localizacao = t1.cd_localizacao WHERE 1=1 AND t1.cd_categoria =".$categoria->getCd()." AND t2.nm_estado ='".$estado."' AND t2.nm_municipio ='".$municipio."' LIMIT ".$init.",".$max."";

$retorno = $this->conexao->query($query) or die(mysql_error());
                     while($anuncio = mysqli_fetch_assoc($retorno))
                     {
                         $anuncios[] = $anuncio;

                     }

var_dump:

"SELECT t1.* FROM tb_anuncio AS t1 JOIN tb_localizacao AS t2 ON t2.cd_localizacao = t1.cd_localizacao WHERE 1=1 AND t1.cd_categoria =1 AND t2.nm_estado ='AC' AND t2.nm_municipio ='Acrelândia' LIMIT 0,17"

Mysql inserir a descrição da imagem aqui

  • In $query I can do in return tbm if you want, actually it is not a var_dump I put a break point in vscode and took the contents of the variable

  • pq WHERE 1=1 ?

  • Just to help with the concatenation

  • Returned null, then the problem is in the query.

  • @LeoCaracciolo https://answall.com/questions/12525/70

  • @Bacco, congratulations, very well explained! Why didn’t I think about it? rs

Show 1 more comment

2 answers

0

Tries to force the printing of errors, maybe giving error only it is not displayed due to the server settings, uses it at the beginning of the code.

ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);

0

I managed to solve the problem, even showing the contents of the variable with the correct values the query lost the formatting when arriving in the bank on account of utf8, I used utf8_decode($municipios) and it worked.

  • Although it is an encoding error, it is good to mark your answer as accepted to close the subject to those who access the post next. The question I already closed, even to avoid new answers-kick.

  • Will or mark, only I need to wait 11 hours to mark my own answer as norreta

  • Yes, there is no hurry, with the post closed there is no problem, it was a suggestion just not to forget even.

Browser other questions tagged

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