bind_param() 的 Mysqli 错误:类型定义字符串中的元素数量与绑定变量的数量不匹配

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

我尽我所能来解决这个错误,但没有成功。 我正在尝试使用准备好的语句动态构建 select 查询:

我有这个代码

$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()) {

}

我遇到了这个错误:

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\xampp\htdocs\cebusingles\search.php on line 42

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

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 = ?

所以我有 2 种类型:is 和 2 个参数:1, USA.

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

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

是否缺少您要插入的变量类型?地点

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

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

types

A string that contains one or more characters which specify the types for the corresponding bind variables: