I have a parameterised SQL PDO query using LIKE in a php script that I can’t get to work.
The original MYSQL version of the script works fine.
And I can get parameterised SQL PDO queries without LIKE to work.
So I think it’s a syntax problem with PDO around the LIKE keyword.
Any help much appreciated!
The parameters are coming from two arrays:
$levels = array('A1', 'A2', 'B1', 'B2', 'C1');
$grades = array('Merit', 'Pass', 'Fail');
The query is
$query1 = "SELECT COUNT(totals.Grade)
FROM totals
WHERE totals.Grade=:grade
AND totals.ExamNumber LIKE :level";
Query processing is:
$result1 = $db->prepare($query1);
$result1->bindParam(':grade', $grade);
$result1->bindParam(':level', "%{$level}%");
$result1->execute();
I’ve tried many variants on the syntax to handle the parameter that needs LIKE. E.g.
totals.ExamNumber LIKE :level
in the actual query
and bindValue
instead of bindParam
and "%$level%"
instead of "%{$level}%"
and different execution:
$result1 = $db->prepare($query1);
$result1->execute(array(':grade' => $grade, ':level' => '%'.$level.'%'));
My results display uses:
$row = $result1->fetch(PDO::FETCH_ASSOC);
if ($row)
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
etc.
The nested loops and the HTML are fine; it’s just that no values are being pulled from the DB to populate the table and cells that the loops generate.