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
Aurélien Grimpard
  • 918
  • 10
  • 24

1 Answers1

0

Sure, something like this should do :

SELECT id, name, replace(name, 'defgh', '<mark>defgh</mark>') as html
  FROM mydb 
  WHERE name LIKE '%defgh%'
Loïc
  • 11,804
  • 1
  • 31
  • 49