2
I am developing a small system that includes javascript and php code, linked to Mysql. I’m trying to send a code that will be set in JS as an argument for a php function.
However, when I test the function with a variable in the SQL search does not work, even if it is an integer variable (tests inserting a normal number in place of the variable work perfectly).
function buscar_informacoes($conexao, $codigo){
$codigo_numerico = (int)$codigo;
echo "Código recebido tem o tipo " . gettype($codigo) . "<br>";
//-> Retorna String
echo "Código alterado tem o tipo " . gettype($codigo_numerico);
//-> Retorna Integer
$sqlBusca = "SELECT * FROM tbl_jogos
WHERE codigo = $codigo_numerico ; " ;
//Quando insere número, funciona normalmente
//Colocando o argumento recebido, não funciona, possui tipo String
//Colocando uma variável que sofreu casting para int, não funciona
$resultado = mysqli_query($conexao, $sqlBusca);
$informacoes_jogo = array();
if($sqlBusca === FALSE) {
die(mysqli_error());
}
while ($informacao = mysqli_fetch_assoc($resultado)) {
array_push($informacoes_jogo, $informacao);
}
return $informacoes_jogo;
//-> Atualmente retorna: array(0) { }
}
The question you don’t want to shut up: why entering a number in the SQL command works and a variable that has an int value obtained from a casting no?
Thanks in advance!
Edit
Query with execution in php:
array(1) { [0]=> array(8) { ["code"]=> string(1) "1" ["name"]=> string(10) "God of War" ["gender"]=> string(15) "Action/Adventure" ["classificaca_indicative"]=> string(40) "Not recommended for children under 18" ["language"]=> string(18) "English/Portuguese" ["platform"]=> string(13) "PlayStation 4" ["mark"]=> string(25) "Playstation - Sony Brasil" ["price"]=> string(6) "148.72" } }
Considering the situation where "it does not work": 1) What is the value of
$codigo_numerico
? 2) What is the final value of$sqlBusca
? 3) What is the error message? (If it does not appear on the screen, check the server log)– Woss
The value of $numeric code is an integer, it can be any one, because it will be multiple passcodes. The test can be done with any integer value. The desirable return of $sqlBusca is an 8 element associative array, including testing the code in the Mysql database works normally. There is no error message, it returns an array, but an empty array.
– Eduardo Galvão
Run directly into the bank
SELECT * FROM tbl_jogos WHERE codigo = 1;
, what is the result? (add to question) And if you do$codigo_numerico = 1
and executeSELECT * FROM tbl_jogos WHERE codigo = $codigo_numerico ;
, what is the result? (add to question)– Woss
Consulta em banco: 
| codigo | nome | genero | classificacao_indicativa | idioma | plataforma | marca | preco |
| 1 | God of War | Action/Adventure | Not recommended for children under 18 | English/Portuguese | Playstation 4 | Playstation - Sony United States | 148.72 |
– Eduardo Galvão
Query with execution in php: array(1) { [0]=> array(8) { ["code"]=> string(1) "1" ["name"]=> string(10) "God of War" ["gender"]=> string(15) "Action/Adventure" ["classificaca_indicative"]=> string(40) "Not recommended for children under 18" ["language"]=> string(18) "English/Portuguese" ["platform"]=> string(13) "PlayStation 4" ["mark"]=> string(25) "Playstation - Sony Brasil" ["price"]=> string(6) "148.72" } }
– Eduardo Galvão
There is the [Edit] button to change the question
– Woss
Other than that, the results were apparently the same. So what really is the problem?
– Woss
The problem is that there is no way to set the integer value in the variable, as done in $numeric code = 1, because this value is received as argument in String format, as "1". Therefore, the $numeric code could receive the argument with the casting, as $codigo_numero = (int)$argumentString, but this way does not work in the sql query. The doubt is the difference between setting $numeric code = 1 or $codigo_numero = (int)$argumentString this way, because the search only happens effectively in the first option.
– Eduardo Galvão
There is no difference if the string received is actually
"1"
. Incidentally, the casting nor is it necessary for consultation.– Woss