Search code examples
mysqlsql-like

transfer several parameters from a form into SQL query


i am working on a table that includes a filter function. For the filter i use a form where i enter the parameters. Those are added to a string which is my SQL query. So far it works fine.

There is oine input field where multiple parameters canbe added. The plan is to seperate them with ; . For example 520;521;522

My plan was to use str_replace to convert this in to sql Code.

For example

$str = str_replace(";", "" OR ", "520;521;522");

Results in to:

SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')

But some how this code does not show the expected results. I only get results for '%520%'

How do i need to adjust this query in order to have the sql query working?

$str = str_replace(";", "" OR ", "520;521;522");

Results in to:

SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')

In another input field i search for names. Here the query looks like this... SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%') This works fine!


Solution

  • The multiple like should be written as,

    SELECT *  
    FROM MaschinenVorgangslisteMitHV  
    WHERE VorgangNr REGEXP '520|522|523';