Php 动态过程语句出错

Php 动态过程语句出错,php,mysql,Php,Mysql,我得到了一个错误:没有为prepared语句中的参数提供数据。但是我已经检查了几个小时的代码,我找不到问题。有什么帮助吗 $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

我得到了一个错误:没有为prepared语句中的参数提供数据。但是我已经检查了几个小时的代码,我找不到问题。有什么帮助吗

$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获得的结果:

一张打印图片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 )

我们错过了绑定和执行$conexion的代码。在那之前,无法帮助你更多。所以$params的值是数组[0]=>i[1]=>4那么为什么我使用它时会说我没有提供数据?:call_user_func_arrayarray$query,'bind_param',$params;我正在提供一个包含2个元素的数组。错误:没有为准备语句中的参数提供数据。都是关于$query->execute;您没有在id\u Center上绑定\u参数是的,我在这里执行:调用\u user\u func\u arrayarray$query,'bind\u param',$params;类似于:$query->bind_parami,4;PHP函数以其名称作为字符串传递。可以使用任何内置或用户定义的函数,但语言构造除外,例如:数组、回显、空、求值、退出、isset、列表、打印或取消设置。无法将此数组传递到第一个参数中的call_ser_func_数组。
Array ( [0] => i [1] => 4 )