0

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:\Server\htdocs\lucky_draw\new.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.

wiki
  • 11
  • 1
    Does this answer your question? [How do you use IN clauses with mysqli prepared statements](https://stackoverflow.com/questions/772913/how-do-you-use-in-clauses-with-mysqli-prepared-statements) – CBroe Nov 26 '20 at 09:21
  • Does this answer your question? [mysqli bind\_param for array of strings](https://stackoverflow.com/questions/17226762/mysqli-bind-param-for-array-of-strings) – Dharman Nov 26 '20 at 09:44
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. – Dharman Nov 26 '20 at 09:44
  • @CBroe still can't configure on how to use it because I have like 4 condition and most of the example out there have like 1 condition. – wiki Nov 26 '20 at 10:07
  • No idea what you are talking about here with your “four conditions”? – CBroe Nov 26 '20 at 10:10
  • @CBroe I believe OP doesn't know how to combine the solution for `IN` with his remaining parameters. – El_Vanja Nov 26 '20 at 10:12
  • Side note: make sure you add a space here: `Status = ?AND ` (between question mark and the logic operator). – El_Vanja Nov 26 '20 at 10:13

0 Answers0