MySQLi Bind Param with an array for IN

前端 未结 2 1073
渐次进展
渐次进展 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:46

    Went through some info (also see: https://stackoverflow.com/a/13253440/165330 ).

    It can but SHOULD NOT be done.

    A way to do it, would use dynamic variables to provide auto-referencable variables and using call_user_func_array to supply a dynamic amount of arguments to the callback/method $stmt->bind_param() .

    prepare('SELECT value1, value2 FROM table1 WHERE value3 IN (' . $s . ')');
    # OR array_map in case of different datatypes
    $typeDefintions = str_repeat( 's' , count( $values ) );
    $params = array( $typeDefinitions );
    foreach ( $values as $k => $v ) {
        ${ 'varvar' . $k } = $v;
        $params[] = &${ 'varvar' . $k };# provide references
    }
    call_user_func_array( array( $stmt , 'bind_param' ) , $params );
    

提交回复
热议问题