作者:青岛二三事-丶儿 | 来源:互联网 | 2023-10-15 11:14
Imtryingtousethisforabasicsearchwithpagination:我正在尝试使用此分区进行基本搜索:$construct?AND?
I'm trying to use this for a basic search with pagination:
我正在尝试使用此分区进行基本搜索:
$cOnstruct= '? AND ? AND..';
$query = $database->prepare('SELECT * FROM something WHERE something LIKE ' . $construct . ' LIMIT :offset, :results');
The only reason I'm mixing them is because unnamed parameters can't have int values because of a PHP bug apparently: https://bugs.php.net/bug.php?id=44639
我混合它们的唯一原因是因为未命名的参数因为PHP错误而无法具有int值:https://bugs.php.net/bug.php?id = 44639
However if I don't mix them, how can I search for a variable amount of terms using bindings?
但是,如果我不混合它们,我如何使用绑定搜索可变数量的术语?
Update
After messing around with it I solved it more or less using named parameters and some loops:
搞砸了之后,我使用命名参数和一些循环或多或少地解决了它:
// build prepared statement
$cOnstruct= '';
for ($x = 0; $x <= $searchArrayCount; $x++) {
$construct .= ($x <$searchArrayCount)
? ":var$x OR name LIKE "
: ":var$x LIMIT :start, :perPage";
}
$query = $database->prepare('SELECT something FROM something WHERE name LIKE ' . $construct);
// bind parameters
for ($x = 0; $x <= $searchArrayCount; $x++) {
$searchArray[$x] = "%$searchArray[$x]%";
$query->bindParam(":var$x", $searchArray[$x]);
}
$query->bindParam(':start', $searchArrayCount, PDO::PARAM_INT);
$query->bindParam(':perPage', $perPage, PDO::PARAM_INT);
If there's a more optimal way of going about this I'd love to be informed.
如果有一种更优化的方式来解决这个问题,我很乐意被告知。
2 个解决方案