【发布时间】:2013-04-21 12:30:06
【问题描述】:
我想做的是在for循环中创建不同的插入语句,并在循环中执行它们。这可能吗?
这是简化代码:
$mysqli = new mysqli("localhost", "user", "password", "database");
for($i=1; $i<10; $i++){
// $query string is created through code, so
// INSERT statement is varying after each loop.
// Lets say, in another step $query will be "INSERT INTO table (row4,row5) VALUES (?,?)";
// $params will be array("value4","value5");
$query = "INSERT INTO table (row1,row2,row3) VALUES (?,?,?)";
$param_type = "sss";
$params = array("value1","value2","value2");
$insert_stmt = $mysqli->prepare($query);
array_unshift($params, $param_type);
call_user_func_array(array($insert_stmt, 'bind_param'), refValues($params));
$insert_stmt->execute();
$insert_stmt->close();
}
如果我运行这段代码,我得到的只是一个插入的行和警告 “call_user_func_array() 期望参数 1 是一个有效的回调,第一个数组成员不是一个有效的类名或对象...”
所以我的问题是:如何通过for循环准备和插入不同的查询和参数?
【问题讨论】:
-
-1 用于发布“简化代码”,这使得整个问题毫无意义。
-
只有 refValues 函数不包括在内,我排除了用于在每个循环中构建不同查询的代码。我认为展示我如何构建查询并不重要。
-
虽然错误信息很清楚。这使得这个问题与stackoverflow.com/questions/15447133/… 重复