以下查询会导致浏览器打印
"does not match number of parameters"
错误类型。
为什么会发生这种情况?
当我替换为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();
}
关于php - MySqli LIKE 查询与参数数量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34223579/