bind_param()出现Mysqli错误:类型定义字符串中的元素数不为';t匹配绑定变量的数量


Mysqli error with bind_param(): Number of elements in type definition string doesn't match number of bind variables

我尽力解决了这个错误,但没有成功。我正试图用准备好的语句动态构建一个选择查询:

我有这个代码

$gm = $_GET['gm']; 
$ct = $_GET['ct']; 
$query = 'SELECT * FROM fchar';
$cond = array();
$params = array();
$type = array();
if (!empty($gm)) {
    $cond[] = "gm = ?";
    $params[] = $gm;
    $type[] = "i";
}
if (!empty($ct)) {
    $cond[] = "ct = ?";
    $params[] = $ct;
    $type[] = "s";
}
if (count($cond)) {
    $query .= ' WHERE ' . implode(' AND ', $cond);
}
if (count($params)) {
    $paramok = implode(', ', $params);
}
if (count($type)) {
    $typeok = implode($type);
}
$stmt = $connection->prepare($query);
$stmt->bind_param(''.$typeok.'',$paramok);
$stmt->execute();

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
while ($stmt->fetch()) {
}

我有这个错误:

警告:mysqli_stmt::bind_param((:类型定义字符串中的元素数与第42行C:''examplep''htdocs''cebusingles''search.php中的绑定变量数不匹配

但当我回显类型和参数时,我得到的是:

echo "<br><br>Types : ".$typeok."<br>Param: ".$paramok."<br><br>Query:   ".$query."<br><br>";
// gives :
// Types : is
// Param: 1, USA
// Query: SELECT * FROM fchar WHERE gm = ? AND ct = ?

所以我有两个类型:is和两个参数:1, USA

我不明白为什么它说类型的数量与参数的数量不匹配。

$stmt->bind_param(''.$typeok.'',$paramok);

缺少您试图插入的变量类型吗?放置

$stmt->bind_param('ss', ''.$typeok.'', $paramok);

您可以在此处阅读使用数字、字符串或类似字符时应使用的字符:
http://php.net/manual/en/mysqli-stmt.bind-param.php

类型

一个字符串,包含一个或多个指定类型的字符对于相应的绑定变量: