php pdo sql query错误:1064 with LIKE

时间:2016-12-21 19:46:30

标签: php mysql pdo

我的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()));

有人能看出我做错了什么吗?

1 个答案:

答案 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.'%');