我的代码在这里遇到了一些真正的困难。我试图将数据数组传递给函数,并使用预处理语句动态构建INSERT语句。
所以我有这个:
public function create($data) {
global $mysqli;
foreach ($data as $field => $value) {
$fields[] = $field;
$values[] = $value;
}
$query = "INSERT INTO " . $this->class_name . " (";
for ($i = 0; $i < count($fields); $i++) {
if ($i == (count($fields) - 1)) {
$query .= "" . $fields[$i] . "";
} else {
$query .= "" . $fields[$i] . ", ";
}
}
$query .= ") VALUES (";
$params = array();
for ($i = 0; $i < count($values); $i++) {
if (is_int($values[$i])) {
$params[] = "i";
} else {
$params[]= "s";
}
if ($i == (count($values) - 1)) {
$query .= "?";
} else {
$query .= "?, ";
}
}
$query .= ")";
if ($stmt = $mysqli->prepare($query)) {
call_user_func_array(array($stmt, "bind_param"), array_merge($params, $values));
} else {
die("COULD NOT CONNECT create()");
}
//echo $query;
}
问题是我一直收到以下错误:
警告:参数2到mysqli_stmt :: bind_param()应该是一个参考,值在第44行的E:\ xampp2 \ htdocs \ school2 \ application \ models \ CRUDAObject.php中给出
我对准备好的陈述很新,但是当我通过它时,我无法确定阵列需要的格式/布局。
有人可以帮忙吗?
答案 0 :(得分:0)
从PHP 5.6开始,这变得非常简单,不需要call_user_func_array。诀窍是使用splat(“ unpack”)运算符( ... )作为值列表。向下滚动以找到 “简短版本” ,或者随时阅读以了解我如何处理SQL事务中涉及的各种数据元素。
我使用编写的三个函数来做三件事。
那之后,这很简单。
因此,首先是基础工作:
// This would be passed to a functionized version of this:
// $MySQLInsertArray is an array of key=>value where key is db column name
// $MySQLDataTypes is a list of data types used to bind, eg: 'sssdsdnb'
// This creates the reference pointers for the insert statement (eg: '?,?,?,?')
$MySQLQs=str_repeat("?,",strlen($MySQLDataTypes)-1)."?";
// Make a *STRING* of the column names
$ColumnList=implode(",",array_keys($MySQLInsertArray));
// Make an *ARRAY WITH NO KEYS* of the values
$ValueList=array_values($MySQLInsertArray);
// Do all the fun SQL stuff
$stmt = $conn->prepare("INSERT INTO ".$TBName." (".$ColumnList.") VALUES (".$MySQLQs.")");
// USE THE UNPACK OPERATOR (...)
$stmt->bind_param($MySQLDataTypes,...$ValueList);
$stmt->execute();
简称为:
所以,这意味着重要的部分是这个和那些神奇的三个时期:
$ stmt-> bind_param($ MySQLDataTypes, ... $ ValueList);