我得到错误"Error: No data supplied for parameters in prepared statement."
,但我已经检查了几个小时的代码,我找不到问题。任何帮助吗?
$sql = "SELECT id FROM users WHERE id_center =?";
$params = array(4);
// This will loop through params, and generate types. e.g. 'ss'
$types = '';
foreach($params as $param) {
if(is_int($param)) {
$types .= 'i'; //integer
} elseif (is_float($param)) {
$types .= 'd'; //double
} elseif (is_string($param)) {
$types .= 's'; //string
} else {
$types .= 'b'; //blob and unknown
}
}
array_unshift($params, $types);
print_r($params);
// Start stmt
$query = $conexion->stmt_init();
$query->prepare($sql);
// Bind Params
call_user_func_array(array($query,'bind_param'),$params);
$query->execute();
printf("Error: %s.'n", $query->error);
这是我得到的print_r($params)
:
Array ( [0] => i [1] => 4 )
一个print_r(array(4))
会帮助你知道pbm是从哪里来的。
让我们遍历一下代码来理解发生了什么:
- $params is defined and look like this : Array ( [0] => 4 )
- in foreach u'll test the value of every row in the array
BUT there is only one
- is_int(4) YES
$types = ' i'
- end foreach
- unshift_array() Will Prepend one or more elements to the beginning $params array
因此$params看起来像:
Array ( [0] => i [1] => 4 )