2

I have a mysql query using PDO, where i would like to use the LIKE statement to search my database for values.

I am currently using the query

$selectdp = $connection->prepare(
    "SELECT `FilePath`,`ItemPicID`,`Extension`
    FROM `ItemPics`
    WHERE `BusinessID`=:bizid
    AND `ItemID`=:itemid
    AND `FilePath` LIKE :search ");

with

$selectdp->bindValue(":search",'%DP');

but i am getting the error

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 '%DP' ....

Things i've tried in the query

  • LIKE=:search
  • LIKE CONCAT('%'.:search)

I've taken a look at

implement LIKE query in PDO and How prepare statement with bindvalue and %? but nothing seems to be working.

Community
  • 1
  • 1
Kenneth .J
  • 1,433
  • 8
  • 27
  • 49

1 Answers1

1

I was taught to concatenate for LIKE expressions within the SQL, and set a parameter within the concatenation. For example:

$selectdp = $connection->prepare(
    "SELECT `FilePath`,`ItemPicID`,`Extension`
    FROM `ItemPics`
    WHERE `BusinessID`=:bizid
    AND `ItemID`=:itemid
    AND `FilePath` LIKE concat('%', :search)

Take a look at Kzqai's answer here, who seems to understand it much more than I do.

From his explanation, a primary difficult with this is preventing the wildcard character from acting as a literal.

Community
  • 1
  • 1
LoganEtherton
  • 495
  • 4
  • 12