0

Is something wrong with the syntax of this query? I'm using mysql and PHP.

$stmt = $conn->query("SELECT denominazione 
                        FROM tab02_impianti 
                        WHERE tab02_impianti.cod_impianto = tab05_workgroup.cod_impianto 
                        AND tab05_workgroup.cod_utente = tab01_utenti.cod_utente ");

Because I get the error:

Fatal error: Uncaught Error: Call to a member function fetch() on boolean in...

The error show up because of the WHERE sentence. It works without it but I get all the data of the table. So this part: "SELECT denominazione FROM tab02_impianti" should be ok.

I cant figure it out, can I get some help please? Thank you very much :)

Let me write something more: I have users table (tab01) and industrial installations (tab02). Every user can be related to more installations and vice versa. The tab05, workgroup, contain the id of both. I just want to show to the user the information about the industrial installations that he is allowed to.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Run that query in phpMyAdmin and if there is an error it will tell you about it. – RiggsFolly Oct 18 '18 at 11:48
  • _Alternatively add some_ **Error checking** but if you cannot be bothered, Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Oct 18 '18 at 11:48
  • You could also make code easier to read and easier to writer if you removed the table numbers part of each tables name and just gave the table a sensible name – RiggsFolly Oct 18 '18 at 11:51

1 Answers1

0

The error you get is caused by fetch() function because your query() function returns false.

The false boolean is return on your query() function because there is an error somewhere on it. To help you debugging your query, you can check for error case :

$stmt = $conn->query("SELECT denominazione FROM tab02_impianti WHERE tab02_impianti.cod_impianto = tab05_workgroup.cod_impianto AND tab05_workgroup.cod_utente = tab01_utenti.cod_utente ");

//error case
if(!$stmt)
{
  die("Execute query error, because: ". print_r($conn->errorInfo(),true) );
}
//success case
else{
     //continue flow
}
André DS
  • 1,823
  • 1
  • 14
  • 24