php

Passing an Array to bind_params

function make_refs_array($arr) {
    $ret = array();
 
    foreach($arr as $key => $value) {
        $ret[$key] = &$arr[$key];
    }
 
    return $ret;
}
 
function run_query() {
    $sql_query .= 'INSERT INTO table_table_table SET firstname=?, lastname=?;';
 
    $params = array('Max', 'Mustermann');
 
    if(!($stmt = $db->prepare($sql_query))) {
        print "\n$sql_query\n";
        echo $db->error . "\n";
        return True;
    }
 
    $params = array_merge(array(str_repeat('s', count($params))), $params);
 
    $bound = call_user_func_array(array(&$stmt, 'bind_param'), make_refs_array($params));
 
    if(!$bound) {
        var_dump($params);
        print "\n$sql_query\n";
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
        return True;
    }
 
    if (!$stmt->execute()) {
        print "\n$sql_query\n";
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        return True;
    }
}