You prepare the statement, but you never execute it.
I donât know where $key is coming from, but the method you use will not prevent injection, with the variable directly in the query.
Here is an example of how to use a prepared statement with a variable LIKE clause.
$key=$_GET['key'];
$like='%$key%';
$sql="SELECT title FROM myTable
WHERE title like ? ORDER BY editDate DESC LIMIT 10";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
while ($searchL=$searchQ->fetch()) {
echo $searchL['title'] .'<br>';
}
$sql = 'SELECT title FROM myTable WHERE title LIKE :key DESC LIMIT 10'; // Set the Search Query:
$stmt = $dbc->prepare($sql); // Prepare the query:
$stmt->execute(['key' => "%" . $key]); // Execute the query with the supplied user's parameter(s):
$records = $stmt->fetchAll();
foreach ($records as $record) {
echo "Title: " . $record['title'] . "<br>";
}
There might be some rewriting of the code as I just converted from an old php sandbox script that I did.