我的PDO查询引发错误
42000 1064 SQL语法错误
$sql = "SELECT * FROM {$this->config->__get('table_medicine')} WHERE patient_id = ? AND medicine LIKE %?%";
$query = $this->dbh->prepare($sql);
$data = array($patient_id, $medicine);
$response = $query->execute($data) or die(implode(" ", $query->errorInfo()));
有人能看出我做错了什么吗?
答案 0 :(得分:1)
%
需要位于LIKE
的字符串参数内。在SQL中使用CONCAT()
:
$sql = "SELECT * FROM {$this->config->__get('table_medicine')}
WHERE patient_id = ? AND medicine LIKE CONCAT('%', ?, '%')";
或在PHP中进行连接:
$data = array($patient_id, '%'.$medicine.'%');