I am trying to select fields with this function
public function discussion (PDO $conn, $name) {
$relatedFallback = function ($limit) use ($name, $conn) {
$like = "%$name%";
$augment = $conn->prepare('SELECT * FROM contents WHERE `type`= ? AND `name` LIKE ? LIMIT ?');
$augment->execute(['discussion', $like, $limit]);
return $augment->fetchAll(PDO::FETCH_ASSOC);
};
$relatedFallback(2); // do other stuff with this value
}
$this->($conn, 'Another topic but with a longer name');
This throws the error
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘2’’
meaning nothing comes from the like
variable hence the LIMIT keyword is exposed to the LIKE keyword. However, I have tried the sql query in phpmyadmin and it does return the intended row. The difference between both queries was, the $like
used in phpmyadmin was coated with quotation marks–precisely
SELECT * FROM contents WHERE
type
= discussion ANDname
LIKE ‘%Another topic but with a longer name%’ LIMIT 2
How may I coat the variable with quotation marks before sending it in the execute method? I’ve tried using backticks but it doesn’t work.