我本地的 Ubuntu Apache2 开发系统运行的是 PHP 8.1,而实时服务器运行的是 7.4 版。我可以将它升级到 8,但只有在我的系统上一切都在这里工作时,目前它不是。关键是我做的任何事情都需要向后兼容早期版本,直到它上线和测试。
为此,在下面的这个片段中,它在
mysqli_stmt::bind_param() does not accept unknown named parameters
上给出了 $stmt->bind_param($types, ...$values);
错误,并且由于我对准备好的陈述非常陌生,所以我不确定该怎么做。我理解一般的错误,但 $Query 是动态的,可能有任意数量的 $values 或者可能只有一个。
$results_array = [];
if (!empty($values)) :
$stmt = $mysqli->prepare($Query);
$stmt->bind_param($types, ...$values);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_array()) :
$results_array[] = $row;
endwhile;
$stmt->close();
return $results_array;
endif;
$mysqli 当然包含连接字符串,而 $Query 是正在使用的动态查询,$types 包含字段类型,即“sssi”或其他任何内容,并匹配 $values 数组中的数据。非常感谢任何帮助!
可以根据值的个数动态生成SQL查询的问号,然后用
mysqli_stmt::bind_param()
将值绑定到参数上
重构代码后,它应该看起来像这样。
$results_array = [];
if (!empty($values)) {
$num_values = count($values);
$question_marks = str_repeat("?,", $num_values);
$question_marks = rtrim($question_marks, ",");
$Query .= " VALUES (" . $question_marks . ")";
$stmt = $mysqli->prepare($Query);
$stmt->bind_param($types, ...$values);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_array()) {
$results_array[] = $row;
}
$stmt->close();
}
return $results_array;