Error trying to join 3 tables

Asked

Viewed 69 times

1

I’m trying to unite three tables in my database, being them Propriedades | Licitacoes | Usuarios. Only every time of the same mistake! he is:

Fatal error: Call to a member function fetchAll() on a non-object in *** on line 61

The goal to which I am trying to join this table is to filter the information at the time of the echo, so much so that I’m using the WHERE at the end of the code. I want that the moment the user enters the page in question show only the results related to it, this relation is given by licitacoes that the user has and Nivel that he owns.

I’ve used almost the same code on another occasion and there was no similar mistake. I don’t know why it’s giving! Can you help me??

<?php
require_once "../conexao.php";
?>

<?php
$rs = $pdo->query(" SELECT a.ID as IDPROP, a.LICITI, a.NOME, a.NIVEL as NIPROP,
                           b.ID as IDLICI, b.ID_LICITI, b.ID_USER,
                           c.ID as IDCHAR, c.USUARIO, c.NIVEL as c.NICHAR 

                    FROM propriedades a INNER JOIN licitacoes b on (a.LICITI = b.ID_LICITI)
                                        INNER JOIN usuario c on (b.ID_USER = c.ID) 
                  ")->fetchAll();

if(!$rs){ print_r($pdo->errorInfo()); }foreach ($rs as $row){

?>

<?php echo $row['IDPROP'];?> = Informações da tabela propriedades
<?php echo $row['IDLICI'];?> = Informações da tabela licitacoes
<?php echo $row['IDCHAR'];?> = Informações da tabela usuario

<?php } ?>
  • Is missing the ON in the INNER JOIN, No? It appears that error?

  • Print the query and run directly to the database see if any errors appear.

  • @Lucas was really missing the ON, but this did not solve! The error I listed in the question! " Fatal error: Call to a Member Function fetchAll() on a non-object in *** on line 61"

  • @rray run directly on the seat? What do you mean?

  • This is because query() failed is usually an error in the query, so you cannot string the fetchAll(). When the query fails is returned the sql state that says the error message, 'category' and code.

  • What the SQL error I meant, sorry

  • It would play the query in a variable and print it, then test in the Workbench or phpmyadmin, $sql = 'select ....'; echo $sql; $pdo->query($sql); ...

  • I think it’s some bad closure! Because the page never finishes loading, it’s like it’s in infinite loop

  • The Fatal error, after you put the ON?

  • @Lucas ta yes. The error is the same!

  • I saw another mistake there: c.NIVEL as c.NICHAR , removes the latter alias, be alone c.NIVEL as NICHAR

  • 1

    @Lucas OBRIGADO. This last mistake that you talk about solved the problem!

  • Show. I will add an answer.

Show 8 more comments

1 answer

1


I found 2 errors in SQL: missing ON us INNTER JOIN and there’s a alias not necessary in c.NIVEL as c.NICHAR.

Follow the correct SQL:

SELECT a.ID as IDPROP, a.LICITI, a.NOME, a.NIVEL as NIPROP,
b.ID as IDLICI, b.ID_LICITI, b.ID_USER,
c.ID as IDCHAR, c.USUARIO, c.NIVEL as NICHAR 
FROM propriedades a
INNER JOIN licitacoes b ON (a.LICITI = b.ID_LICITI)
INNER JOIN usuario c ON (b.ID_USER = c.ID)

Since SQL was incorrect, the function query() returned false and tried to call straight fetchAll(), but how false is not an object, PHP launches this Fatal error.

To circumvent this type of situation, it is interesting to store the query in a variable and test it, for example:

$res = $pdo->query('...');
if ($res === false) {
    // erro na consulta
} else {
    $lista = $res->fetchAll();
}

Browser other questions tagged

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