How to make prepared statement for where IN in query?

  mysql, php

I’m trying to create a prepared statement that have many condition
this is my original query

$sql ="SELECT name, tran, no
    FROM abcd 
    WHERE type = 'P' 
    AND Status = '0'
    AND  id IN('acv12', 'qty1', 'obm2') AND date(date) >= '2019-12-18'";

this is what i have tried

 $param_type = 'P';
 $param_Status ='0';
 $param_date = '2019-12-18';

 $ids = ['acv12', 'qty1', 'obm2'];
 $clause = implode(',', array_fill(0, count($ids), '?'));
 $param_id = $clause;

 $stmt = $link->prepare("SELECT name, tran, no FROM abcd WHERE type = ? AND Status = ?AND  id IN ($clause) AND date(date) >= ? ");
 $stmt->bind_param('siss',$param_type, $param_Status,$param_id, $param_date);
 $stmt->execute();

 $result = $stmt->get_result();
 while ($row = $result->fetch_assoc())
        {
            $data[] = $row;
            $name= $row['name'];
            $tran = $row['tran'];
            $no = $row['no'];
}

i got error with

Warning: mysqli_stmt::bind_param(): Number of variables doesn’t match
number of parameters in prepared statement in ….

and

Uncaught Error: Call to a member function fetch_assoc() on bool in
D:Serverhtdocslucky_drawnew.php:46 Stack trace: #0 {main} thrown
in

I can’t figured it put how to fix this since i’m just stating to using prepared statement.

Source: Ask PHP

LEAVE A COMMENT