MySQLi Bind Param with an array for IN

前端 未结 2 1077
渐次进展
渐次进展 2020-11-27 07:46

I am trying to pass an array to $stmt->bind_param for as an IN variable. How can I do this?

$values = array(\'a\',\'b\',\'c\',\'d\');
$values         


        
2条回答
  •  伪装坚强ぢ
    2020-11-27 08:43

    This is a scenario where doing it this way is inappropriate. You're constructing actual SQL (that's what the commas and quotes are), and passing it in as a parameter. It's basically evaluating to value3 IN ('...') where ... is the entirety of $values.

    Also that's a good call about the quotes. MySQL uses single quotes.

    You'll need to either build the SQL using string concatenation alone, or use more than one parameter.

    EDIT

    As an example:

    $values = array('a','b','c','d');
    $values = "'" . implode("','", $values) . "'";
    $stmt->prepare('SELECT value1, value2 FROM table1 WHERE value3 IN (' . $values . ')');
    

提交回复
热议问题