Search code examples
phppdosql-like

Multiple LIKE in PDO query


I need to perform a query on PDO with multiple "likes".

So far I would say my query for 1 like would look like this: $query = $database->prepare('SELECT * FROM table WHERE column1 LIKE ?'); $query->execute(array('%$value1%'));

So, in case I had this in mysql:

mysql_query("SELECT * FROM table WHERE column1 LIKE %$value1% OR column1 LIKE %$value2% OR column2 LIKE %$value1%")

How would it translate in PDO?

mysql_query("SELECT * FROM table WHERE column1 LIKE ? OR column1 LIKE ? OR column2 LIKE ?")
$query->execute(array('%$value1%','%$value2%',.....));

Would that be good enough?


Solution

  • That is pretty much it, although you still appear to be using the mysql_* functions rather than PDO in your second example.

    Apart from that you'll just need to remember to use double quotes if you want your variables to substitute into the string.

    $query = $dbh->prepare('SELECT * FROM table WHERE column1 LIKE ? OR column1 LIKE ? OR column2 LIKE ?');
    $query->execute(array("%$value1%","%$value2%",.....));