SQL Query to look at "name like %?%" instead of "name like ?"

As you will see from the code below I am trying to build a SQL query based on which form elements are submitted. Currently the code works by using, name like ?, however I would like to use name like %?% in my query.

if ((isset($_POST[‘name’])) and (preg_match(“/[1]+$/”, $_POST[‘name’]))) {
$conditions[‘name like ?’] = [‘s’, $_POST[‘name’]];
$criteria = "Name is " . $_POST[‘name’];

The only way I can get this to work is by amending the post data like this…
$_POST[‘name’] = “%” . $_POST[‘name’] . “%”;

Is this the best solution?

P.S. I am using $ underscore POST but the site removes it for some reason :confused:

  1. a-zA-Z ↩︎

Underscore is used to indicate italics. (_italics_) To keep the underscore, use the preformat button at the top of the editor (highlight the text to preformat, click the button that looks like “</>” without the quotes.)



If you’re Preparing statements, it’s the ONLY solution. The %'s need to be inside the quote marks. Effectively, if you try to put them in as name LIKE %?%, what goes out to the server is name LIKE %“aname”%, which isnt going to work.


Ok, that answers my question.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.