MySqli LIKE查询与参数数目不匹配


MySqli LIKE query not matching number of parameter

以下查询导致浏览器打印

"与参数数量不匹配"

错误类型。

为什么会发生这种情况?

当我用LIKE '%".$country."%'替换并去掉bind_param时,它不会带来任何错误。

$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE '%?%'
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s", $country);
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}

我想准备语句,而不是将原始数据插入查询中。

%必须是值的一部分:

$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE ?
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s","%".$Country."%");
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}

您可以像一样使用它

$country = "%{$country}%";
$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE ?
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s", $country);
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}