我的参数错误无效,但我猜我的参数数量正确。
有人在这看到相反的情况吗?
我收到此错误:
PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与$ readNews-> execute();
中的标记数不匹配 if(isset($_POST['search']))
{
$search = $_POST['search'];
$readNews = $pdo->prepare("SELECT * FROM news WHERE title LIKE ? ORDER BY date DESC LIMIT ?, ?");
$readNews->bindValue(1, $search);
$readNews->bindValue(1, $begin,PDO::PARAM_INT);
$readNews->bindValue(2, $max,PDO::PARAM_INT);
}
else
{
$readNews = $pdo->prepare("SELECT * FROM news ORDER BY date DESC LIMIT ?, ?");
$readNews->bindValue(1, $begin,PDO::PARAM_INT);
$readNews->bindValue(2, $max,PDO::PARAM_INT);
}
$readNews->execute();
答案 0 :(得分:3)
您有三个参数,但您为索引1
分配了两次值。试试这个......
$readNews->bindValue(1, $search);
$readNews->bindValue(2, $begin,PDO::PARAM_INT);
$readNews->bindValue(3, $max,PDO::PARAM_INT);
我猜你可能想要在$search
值周围包装一些通配符。试试这个......
$readNews->bindValue(1, "%$search%");
...或在您的查询中使用CONCAT
...
WHERE title LIKE CONCAT('%', ?, '%')
您的查询还应使用WHERE
代替AND
...
SELECT * FROM news WHERE title LIKE ? ORDER BY date DESC LIMIT ?, ?"
我可能会通过使用命名占位符来删除一些重复来简化这一过程。总的来说,这......
if (isset($_POST['search'])) {
$stmt = $pdo->prepare("SELECT * FROM news WHERE title LIKE CONCAT('%', :search, '%') ORDER BY date DESC LIMIT :begin, :max");
$stmt->bindParam(':search', $_POST['search']);
} else {
$stmt = $pdo->prepare('SELECT * FROM news ORDER BY date DESC LIMIT :begin, :max');
}
$stmt->bindParam(':begin', $begin, PDO::PARAM_INT);
$stmt->bindParam(':max', $max, PDO::PARAM_INT);
$stmt->execute();