mysqli preprade statement og variabelt antal parametre er lidt tricky.
Eksempel:
<?php
function load($con, $sqlstr, $typs, $vals) {
if($stmt = $con->prepare($sqlstr)) {
call_user_func_array(array($stmt, 'bind_param'), array_merge(array($typs), array_map(function(&$v) { return $v; }, $vals)));
$stmt->execute();
if($res = $stmt->get_result()) {
$retval = $res->fetch_all(MYSQLI_ASSOC);
$res->free();
$stmt->close();
} else {
die($con->error);
}
} else {
die($con->error);
}
return $retval;
}
function test($con, $sqlstr, $typs, $vals) {
$arr = load($con, $sqlstr, $typs, $vals);
foreach($arr as $row) {
echo $row['f1'] . ' ' . $row['f2'] . "\r\n";
}
}
$con = new mysqli('localhost', 'root', '', 'Test');
if(mysqli_connect_errno()) die(mysqli_connect_error());
test($con, 'SELECT f1,f2 FROM t1 WHERE f1=?', 'i', array(2));
test($con, 'SELECT f1,f2 FROM t1 WHERE f1=? OR f1=?', 'ii', array(1,3));
$con->close();
?>