I am trying to use this for a basic pagination search:
$construct = '? AND ? AND..'; $query = $database->prepare('SELECT * FROM something WHERE something LIKE ' . $construct . ' LIMIT :offset, :results');
The only reason I mix them is that unnamed parameters cannot have int values โโdue to a PHP error, obviously: https://bugs.php.net/bug.php?id=44639
However, if I do not mix them, how can I search for a variable number of terms using bindings?
Update
After messing with it, I decided to more or less use 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 is a better way around this, I would like to be informed.
source share