Home > Database > Mysql Tutorial > Can SQL Parameters Be Used Effectively with LIKE Statements?

Can SQL Parameters Be Used Effectively with LIKE Statements?

DDD
Release: 2025-01-02 16:24:39
Original
826 people have browsed it

Can SQL Parameters Be Used Effectively with LIKE Statements?

Using Parameters with the LIKE Statement in SQL

When developing a search function, it's crucial to minimize potential security risks like SQL injection attacks. One approach is using parameters in SQL queries. However, users may encounter issues when employing parameters with LIKE statements.

The following query demonstrates the intended parameter usage in the LIKE statement:

SELECT * FROM compliance_corner WHERE (body LIKE '%@query%') OR (title LIKE '%@query%')
Copy after login

But, this query doesn't produce any results. This prompts the question: are parameters applicable in this context, or are they limited, as seen in this instance?

SELECT * FROM compliance_corner WHERE body LIKE '%<string>%'
Copy after login

Additionally, the user has provided an alternative query that returns results in SQL Server:

SELECT * FROM compliance_corner WHERE (body LIKE '%max%') OR (title LIKE%max%')
Copy after login

To effectively use parameters with the LIKE statement, it's recommended to reference the VB.NET code snippet below:

Dim cmd as New SqlCommand( _
"SELECT * FROM compliance_corner" _
+ " WHERE (body LIKE @query )" _
+ " OR (title LIKE @query)")

cmd.Parameters.Add("@query", "%" +searchString +"%")
Copy after login

The above is the detailed content of Can SQL Parameters Be Used Effectively with LIKE Statements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template