Escaping ‘%’ in MySQL LIKE statement when sprintf

I wanted to run a SQL query against MySQL database server which contains search string and need to be formated using sprintf. The problem arise when format. It is because of the sign “%” am using to advance my search term. The query was…

Here where the error fires. Thanks God, I found the solution for it.

This can be handled simply as follows,

but for a query like below, where it need multiple formating due to it complex and dynamic generation, I managed to prepare like below and worked fine for me.

Before generating the next %, need to double the current % where it will become single % after formated.

Note this

Leave a Reply

Your email address will not be published. Required fields are marked *


Please wrap all source codes with [code][/code] tags.

This site uses Akismet to reduce spam. Learn how your comment data is processed.