Error trying to join 3 tables

1

I'm trying to join three tables from my database, being Propriedades | Licitacoes | Usuarios . Only every time the same mistake is made! it is:

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

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

I've used pretty much the same code on another occasion and it has not given a similar error. I do not know why this is happening! 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 } ?>
    
asked by anonymous 11.02.2015 / 20:10

1 answer

1

I found 2 errors in SQL: ON is missing in INNTER JOIN and there is an alias not needed 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 query() function returned false and tried to call fetchAll() direct, but since false is not an object, PHP throws this Fatal error .

To get around this kind 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();
}
    
11.02.2015 / 20:37