I have a table containing lots of data and one of them is a blob. I some times needs to look into this blob for data using PHP.
I do:
select `desc` f
This query:
SELECT
CONCAT(
'...',
SUBSTR(`description`,
LOCATE('Nam rhoncus', `description`) - 10,
(LENGTH('Nam rhoncus') + 20)),
'...') AS `description`
FROM table
WHERE `description` LIKE '%Nam rhoncus%';
(I broke it down like this so it's easier to read)
this will output:
...m auctor. Nam rhoncus, purus eg...
So in your PHP you can do:
<?php
define('CHAR_LEFT', 10);
define('CHAR_RIGHT', 10);
// db stuff
$search = mysql_real_escape_string($search_var);
$query = "SELECT CONCAT('...', SUBSTR(`description`, LOCATE('" . $search . "', `description`) - " . CHAR_LEFT . ", (LENGTH('" . $search . "') + " . (CHAR_LEFT + CHAR_RIGHT) . ")), '...') AS `description` FROM table WHERE `description` LIKE '%" . $search . "%';";
// then your request
NOTE: Ill be careful using mysql reversed words, this is why I use description
instead.
Something like the following should do what you need:
SELECT
SUBSTR(description, INSTR(description, 'Nam rhoncus'), LENGTH('Nam rhoncus')) matchStr
FROM
testTable
WHERE
description like '%Nam rhoncus%'
;