I'm starting in the PHP language, and I need to return a simple query made in sql from a variable that gets the value entered by the user. Being that, it will only display the result of the primary key search. But in the code below what you type in the $buscar
field falls right into else
, such that if all the value that I put in the primary key in the $buscar
field was null
. Can you help me?
Note: The query in sql is working, ie: select * from empresa where cod = 123;
<?php
$buscar = $_POST['buscar'];
$sql = mysqli_query ($conexao, "SELECT * FROM loja where COD = '%$buscar%' ");
$row = mysqli_num_rows($sql);
if ($row != 0 && $row < 900) {
while($linha = mysqli_fetch_array ($sql)) {
$nome = $linha ['nome'];
$contato = $linha['contato'];
$endereco = $linha['endereco'];
$coordenador = $linha['coordenador'];
echo "<br /> <br />";
echo "<strong> Contato: </strong>".$nome;
echo "<strong> Contato: </strong>".$contato;
echo "<strong> Coordenador: </strong>".$coordenador;
echo "<strong> Endereço: </strong>".$endereco;
}
} else {
echo "Desculpe nenhum registro encontrado";
}
?>