Error Querying in MYSQL [duplicate]

0

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.

    
asked by anonymous 21.06.2016 / 02:51

2 answers

1

Your query is wrong and the function is returning false . It is always good to check after the query if it was done correctly.

$resultado = mysqli_query($conectar,$query);
if( !$resultado )
{
    die("Consulta falhou.");
}

And I believe you want to return the variable $retorno in the BuscaAlgo() function.

If you want to JOIN with more than one reference use INNER JOIN PRANCHA AS PRAN ON USU.exp = PRAN.exp_ref AND USU.altura = PRAN.altura_ref AND USU.peso = PRAN.peso_ref

    
21.06.2016 / 02:55
0

You are using the PRANCHA AS PRAN table in the WHERE but only set it to INNER JOIN, you should set it as follows:

SELECT  USU.usuario,
             USU.nome,
             USU.exp,
             USU.altura,
             USU.peso,
             PRAN1.exp_ref,
             PRAN1.altura_ref,
             PRAN1.peso_ref,
             PRAN1.tipo_prancha,
             PRAN1.tamanho_prancha, 
             PRAN1.meio_prancha, 
             PRAN1.litragem_prancha

             FROM

             PRANCHA AS PRAN1,

             DADOS_USUARIO AS USU 

             INNER JOIN PRANCHA AS PRAN2 on USU.exp = PRAN2.exp_ref

              WHERE

              USU.altura = PRAN1.altura_ref

              AND USU.peso = PRAN1.peso_ref

              ORDER BY USU.usuario DESC LIMIT 1
    
21.06.2016 / 03:02