Read data with SQL LIKE operator

How to use the SQL LIKE operator for partial name searches in ChronoForms.

Overview

The issue occurs because the wildcard characters are not being properly appended to the search parameter within the SQL query.
Create a separate variable that adds the percentage signs to the search term and then use the {var.quote:variable_name} placeholder in your SQL query to safely include the formatted value.

Answered
ChronoForms v8
ba 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

ba 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 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.
This topic is locked and no more replies can be posted.