Error while searching in MYSQL

Asked

Viewed 211 times

0

I am trying to make a query and return to the user via Ajax and is being displayed the following errors:

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);
      }               
    });
  });
});

PHP QUERY.

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"];  
}   

PHP BANK.:

<?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?

FOUND THE MISTAKE

The error was in the tables of my bank. Some of them were different from the ones I had stated in SELECT. Problem solved. Both the SELECT how much its return is correct and functional.

2 answers

1

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

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

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

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

  • I found the error. It was in the tables of my bank, were declared with different names of my SELECT. Thank you for your willingness to help!

0

You are using the table BOARD AS PRAN in WHERE but only set it to INNER JOIN, you must 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
  • Then he’ll do two JOINS...

Browser other questions tagged

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