当我尝试下面的代码时,它会给我一个警告
mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
$type = "ss";
$param = array("Maq","bool");
$params[] = &$type;
$params[] = &$param;
call_user_func_array( array($stmt, 'bind_param'), $params );
我真的不知道为什么会发生这种情况我在stackoverflow中读了很多相同的标题问题,但是有些人用逗号分隔类型或者需要更多类型但是在我的情况下我认为它是正确的,但为什么我一直在收到警告?
答案 0 :(得分:4)
$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
$stmt->bind_param('ss', $param[0], $param[1]);
$stmt->execute();
//other version
$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
$type = "ss";
$names = array("Maq", "bool");
$params = array(&$type, &$names[0], &$names[1]);
call_user_func_array(array($stmt, 'bind_param'), $params);
答案 1 :(得分:1)
这是一个迟到的答案,我希望能帮助别人。
这是我的mysqliLayer类的一部分,所以我提取了所需的变量。
// establish mysqli connection
$conn = new mysqli(.....);
$tableName = 'users';
// Types to bind
$type = 'isss';
$fields = ['id','name', 'email', 'created'];
$values = [1, 'angel', 'email@test.com', '2018-1-12'];
$sql = "INSERT INTO " . $tableName . " (" . join(',', $fields) . ") VALUES (?,?,?,?)";
$stmt = $conn->prepare($sql);
// Using ...token introduced in php v.5.6 instead of call_user_func_array
// This way references can be omitted, like for each value in array
$stmt->bind_param($type, ...$values);
$stmt->execute();
$stmt->close();