Skip to content
Advertisement

Using LIKE statement in PHP

I am setting up a new API, I am wanting to display selected info from a Mysql database with a ‘SELECT columns WHERE column LIKE % ?’, but I’am getting error in ‘%’ symbol, where I must insert ‘%’ this symbol ?

This is my query in folder models

This is how i execute that

And this is how I request it

Advertisement

Answer

When using a LIKE clause with a parameterised query in a prepared statement, simply put the wildcard symbol in the param you are appending.

For example, if your query was:

In PHP, you would define the parameter in this way:

Of course, you can have the ‘%’ at only the start, or only the end, or both, depending on how you want the wildcard to work:

As an aside, you can also put it into the variable:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement