Fatal error: Call to a Member Function fetch_array() on Boolean when querying Function help

Asked

Viewed 3,306 times

0

I am doing a function that when it receives a number , in case, an id it makes a query and rescues the name that is in that id and I come across this error.

code

function catporid($cat){

    global $aCon;

    $sql = 'SELECT cat_nome FROM cat WHERE cat_id = $cat';  

    $query = $aCon->query($sql);   

    $r = $query->fetch_array();    

    $cat = $r['cat_nome'];  

    return $cat."id";

}
  • Apply correction to variable $cat using quotation marks, so what you get is a bool instead of the desired object.

  • fixed only that the error still remains $sql = "SELECT cat_name FROM cat WHERE cat_id = '$cat'";

  • Look at the answer, I’ve presented two ways you can fix that.

  • Related, if not duplicated: https://answall.com/questions/28184/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in

  • @Edilson solved ! was a column with a different name and changed the code a little .. thanks !

  • Even if an answer has already been chosen, I would like to add an important tip. Whenever such an error occurs it is because there is a syntax error in the sql query. Instead of guessing where the error is, just use the connection variable to return the last error. Something like this: echo $conexao->error;

Show 1 more comment

2 answers

1


Use braces { to work with variables inside quotes:

$sql = "SELECT cat_nome FROM cat WHERE cat_id = '{$cat}'";

Or you can do this too:

$sql = "SELECT cat_nome FROM cat WHERE cat_id = '". $cat . "'";

It is also recommended that you check whether results of this query have been returned before trying to print or capture any value.

function catporid($cat){
    global $aCon;
    $stmt = "SELECT cat_nome FROM cat WHERE cat_id = '{$cat}'";

    if($query = $aCon->query($stmt)){
        if($query->num_rows > 0){
            $r = $query->fetch_array();
            return $r['id'];
        }
    }
    return false;
}
  • made the modification, does not accuse errors but not the result. needs more information ?

  • @Drbs still have bugs ?

0

According to php documentation, Mysqli :: query ():

Returns FALSE in case of failure. For queries SELECT, SHOW, DESCRIBE or successful EXPLAIN mysqli_query() will return an object mysqli_result. For other successful queries mysqli_query () return TRUE. This means that the following query is failing

 $mysqli->query($bup);

Which means that some statement in sql in a variable is causing an error. I recommend revision or desktop test. It seems that the mistake is not a mistake of syntax (since a syntax error would have caused a even more complex error), which means that Mysql can read its statement, but the operation is failing for some reason. You will have that review your SQL statement as well as your table and see what the failure in logic is.

Source: Source 01

Browser other questions tagged

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