I am trying to query and return to the user via Ajax and the following errors are being displayed:
Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, boolean given in line 77 - >
while($experiencia = mysqli_fetch_assoc($resultado)){
Warning: Invalid argument supplied for foreach () in line 89 - >
foreach($resultado as $valor){
MY AJAX:
jQuery(document).ready(function($){
$('.enviar').click(function(){
$.ajax({
url: '<?php bloginfo("template_url") ?>/consulta.php',
type: 'POST',
data: 'nome=' + $("#nome").val() + '&email=' + $("#email").val() + '&estilo=' + $("#estilo").val() + '&experiencia=' + $("#experiencia").val() + '&altura=' + $("#altura").val() + '&peso=' + $("#peso").val(),
error: function(){
alert('ERRO!!!');
},
success: function(data){
$('#resultado').html(data);
}
});
});
});
CONSULTA.PHP
include "banco.php";
function BuscaAlgo($conectar){
$query = "SELECT USU.usuario,
USU.nome,
USU.exp,
USU.altura,
USU.peso,
PRAN.exp_ref,
PRAN.altura_ref,
PRAN.peso_ref,
PRAN.tipo_prancha,
PRAN.tamanho_prancha,
PRAN.meio_prancha,
PRAN.litragem_prancha
FROM DADOS_USUARIO AS USU
INNER JOIN PRANCHA AS PRAN
on USU.exp = PRAN.exp_ref
WHERE USU.altura = PRAN.altura_ref
AND USU.peso = PRAN.peso_ref
ORDER BY USU.usuario DESC LIMIT 1";
$resultado = mysqli_query($conectar,$query);
$retorno = array();
while($experiencia = mysqli_fetch_assoc($resultado)){
$retorno[] = $experiencia;
}
return $resultado;
}
$resultado = array();
$resultado = BuscaAlgo($conectar);
foreach($resultado as $valor){
echo $valor["usuario"]; print(". . . .");
echo $valor["nome"]; print(". . . .");
echo $valor["exp"]; print(". . . .");
echo $valor["altura"]; print(". . . .");
echo $valor["peso"]; print(". . . .");
print("///////");
echo $valor["tipo_prancha"]; print(". . . .");
echo $valor["tamanho_prancha"]; print(". . . .");
echo $valor["meio_prancha"]; print(". . . .");
echo $valor["litragem_prancha"];
}
BANK.PHP:
<?php
$bdServidor = '127.0.0.1';
$bdNome = 'word1';
$bdUsuario = 'root';
$bdSenha = '';
$conectar = mysqli_connect($bdServidor,$bdUsuario, $bdSenha, $bdNome);
if (mysqli_connect_errno($conectar))
{
echo "Problemas para conectar no banco. Verifque os dados!";
die();
}
What am I doing wrong?
I FOUND THE ERROR
The error was in the tables in my bank. Some of them were different from the ones I had declared in SELECT
. Problem solved. Both the SELECT
and its return are correct and functional.