Read data with SQL LIKE operator

bart99.hermans266 30 Dec, 2024

Hi Max,

I would like to create a form in which an admin initially searches for a Joomla user by entering a part of the name in a search field, and afterwards they will do some modifications on this record.

So on the first page I created a text field in which they will enter a part of the name of the user they are looking for. I don't want them to have to enter the full name as it is prone to spelling mistakes and it takes a lot of time. On the next page an SQL query is executed with this text value as parameter in the WHERE clause and it shows the result in a table.

In order to give the user the freedom to enter a part of the name I use the LIKE operator with the % sign.However, the SQL query never returns any records. When I simply don't use the LIKE operator but just the = operator, it works correctly (but I have to enter the full name) in the search field. The SQL query (with the LIKE operator) also works fine when I execute it as a test in the database directly.

Am I forgetting something?

Please see the attached images.

Best regards,

Bart

bart99.hermans266 30 Dec, 2024
Answer
1 Likes

Hi Max,

Actually I already found a workaround 😅

I added the % signs by adding a variable, and used this variable in the SQL query.

Best regards,

Bart

Max_admin 31 Dec, 2024
1 Likes

correct, but you should modify the SQL query to be like this:

LIKE {var.quote:zoeknaam_wildcard}

this is in order to prevent SQL injection, the variable value will be safely quoted by mysql

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.