1

I have this kind of data in my database "mydb" :

id | name
---------
1  | abcdef
2  | bcdefg
3  | cdefgh
4  | defghi
5  | efghij

I do a simple SELECT query like this one :

SELECT * FROM mydb WHERE name LIKE '%defgh%'

So i get those results :

id | name
---------
3  | cdefgh
4  | defghi

I would like to know if there is a way, with mysql, to inject html code which encompasses the string searched with "LIKE" statement directly in the result ?

So the results would be :

id | name   | html
----------------
3  | cdefgh | c<mark>defgh</mark>
4  | defghi | <mark>defgh</mark>i
1
  • 1
    replace(name, 'defgh', '<mark>defgh</mark>') Commented Aug 22, 2023 at 14:34

1 Answer 1

0

Sure, something like this should do :

SELECT id, name, replace(name, 'defgh', '<mark>defgh</mark>') as html
  FROM mydb 
  WHERE name LIKE '%defgh%'
Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes that will do ! But i use PDO prepared query ... i should have mention it. I think it will be better to do another question ...
Here is the other question : stackoverflow.com/questions/76954518/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.