PHP Fatal error: Call to a member function bind_param () on boolean

2

I've looked at it more than 100 times. Somebody sees the error. I'm trying to make an update.

public function modificar_producto($datos) {
    $ok = "false";
    $sql = "update productos set "
        . "codigo_articulo=?,"
        . "titulo_producto=?,"
        . "contenido_producto=?,"
        . "precio_producto=?,"
        . "cantidad_disponible=?,"
        . "imagen_producto=?,"
        . "categoria=?"
        . "where id_producto=?";
    $stmt = $this->consulta_prepared($sql);
    $stmt->bind_param("sssiissi",$datos[0], $datos[1], $datos[2],$datos[3],$datos[4],$datos[5],$datos[6],$datos[7]);
    $stmt->execute();
    if ($stmt) {
        $ok = "true";
    }
    $arr = array('ok' => $ok);
    return ($arr);
}

this is the error that happens to me:

  

[13-Feb-2017 05:08:39 Europe / Berlin] PHP Fatal error: Call to a   member function bind_param () on boolean in   C: \ xampp \ htdocs \ zone \ admin \ classes \ products.class.php on line 146

    
asked by Cesar 13.02.2017 в 05:12
source

2 answers

1

The meaning of PHP Fatal error: Call to a member function bind_param() on boolean comes from not taking into account that $stmt could be worth false (a Boolean value, not a mysqli resource) in case of an error in prepare .

A Boolean data has no properties or methods (like the method called bind_param() that you try to use). Hence the message: "You can not call a member function of a Boolean data".

You can find out what is wrong with your code if you shield it correctly from error situations. It is a bad practice (and will cause situations like this) not to do it.

Obviously the error comes from having incorrectly concatenated the character strings because "categoria=?" . "where id_producto=?" results in "categoria=?where id_producto=?" , a SQL syntax error.

In the following example I show you how to shield your code before error conditions and avoid the concatenation of strings, prone to this type of errors:

public function modificar_producto($datos) {
    /* No es necesario concatenar cadenas, se puede hacer de esta manera */
    $sql = '
      UPDATE productos SET
        codigo_articulo=?,
        titulo_producto=?,
        contenido_producto=?,
        precio_producto=?,
        cantidad_disponible=?,
        imagen_producto=?,
        categoria=?
      WHERE id_producto=?
    ';
    $stmt = $this->consulta_prepared($sql);
    /* Comprobamos si la preparación se finalizó con éxito o hubo error */
    if ($stmt === false) {
        /* Puedes hacer un return con ok a false o lanzar una excepción */
        /*throw new Exception('Error en prepare: ' . $stmt->error);*/
        return [ 'ok' => 'false' ];
    }
    $stmt->bind_param('sssiissi',
      $datos[0],
      $datos[1],
      $datos[2],
      $datos[3],
      $datos[4],
      $datos[5],
      $datos[6],
      $datos[7]
    );
    $stmt->execute();
    if ($stmt === false) {
        /* De nuevo puedes lanzar una excepción o devolver un error */
        /*throw new Exception('Error en execute: ' . $stmt->error);*/
        return [ 'ok' => 'false' ];
    } else {
        return [ 'ok' => 'true' ];
    }
}

I, personally, prefer the use of exceptions. When you call the function modificar_producto you can control an error condition easily:

try {
  modificar_producto($datos);
  /* Todo fue OK si llegamos a esta línea */
} catch (Exception $e) {
  /* Podemos finalizar la ejecución con un mensaje o mostrar HTML con él */
  die('Error modificando producto: ' .  $e->getMessage());
}

Returning an array with an element ok to true or false will prevent or make it difficult to debug the source of the error.

    
answered by 04.01.2018 / 08:29
source
0

Your problem is that $ this- > query_prepared ($ sql) always returns false since your query is not correct because there is a missing space before where (you are concatenating the variable? with where).

Basically you are doing: Update products set category =? where product_id =?

Always put a space at the beginning and end of each line between "" and so you will never get that error.

$sql = " update productos set "
    . " codigo_articulo=?, "
    . " titulo_producto=?, "
    . " contenido_producto=?, "
    . " precio_producto=?, "
    . " cantidad_disponible=?, "
    . " imagen_producto=?, "
    . " categoria=? "
    . " where id_producto=? ";
    
answered by 13.02.2017 в 08:43