Error adding 'NOT IN' in a query of prepared statements

1

By adding NOT IN in a query:

$stmt = $conexion->prepare("SELECT producto,detalle 
                            FROM producto 
                            WHERE activo=? NOT IN ($producto) 
                            order by id_producto ASC limit 10");
$stmt->bind_param("i",$activo);
$activo = "1";

I get the following error:

  

Fatal error: Call to a member function bind_param () on boolean in C: \ xampp \ htdocs \ ecommerce \ products.php on line 9

The line of the error corresponds to the following line

$stmt->bind_param("i",$activo);
    
asked by Carlos 16.07.2018 в 22:42
source

4 answers

0

You must take into account that you are applying prepared sentences.

Therefore you must add in the query or variable direct $ or the symbol ? in the query.

Therefore the query would be as follows:

$stmt = $conexion->prepare("SELECT producto,detalle 
                            FROM producto 
                            WHERE activo=? NOT IN (?) 
                            order by id_producto ASC limit 10");
$stmt->bind_param("ii",$activo,$producto);
$activo = "1";

In the bind_param you must add the following:

$stmt->bind_param("ii",$activo,$producto);

As you indicate that the variable $producto is the id of a product, the value i is added if it is text you add the value of s .

    
answered by 16.07.2018 / 23:38
source
0

Your sentence is poorly constructed. I think you need two separate conditions. Something like this:

$stmt = $conexion->prepare("SELECT producto,detalle 
                        FROM producto 
                        WHERE activo=? AND activo NOT IN ($producto) 
                        order by id_producto ASC limit 10");
    
answered by 16.07.2018 в 22:46
0

As @ alanfcm points out, it is badly built, but apart from that, the $ product variable could be malformed, if you tell us what it contains help.

On the other hand it seems that this:

$stmt = $conexion->prepare("SELECT producto,detalle 
                        FROM producto 
                        WHERE activo=? NOT IN ($producto) 
                        order by id_producto ASC limit 10");

is returning a Boolean value because later when doing

$stmt->bind_param("i",$activo);

The gives the error

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

What makes me think that the $ connection-> prepare fails and returns false or that the same returns true as a state and you should execute the following line.

$conexion->bind_param("i",$activo);

If you print to us that contains the value $ stmt, it would help us to know more.

    
answered by 16.07.2018 в 22:58
0

Asking a little and with how little or much you show us, I come to the conclusion that you are misusing NOT IN since you are comparing the input parameter with one with the.

$stmt = $conexion->prepare("SELECT producto,detalle 
                        FROM producto 
                        WHERE activo=? AND id_producto NOT IN ($producto) 
                        order by id_producto ASC limit 10");

Look like this and tell us

I hope it serves you

    
answered by 16.07.2018 в 23:29