I am afraid that I don’t know how to apply your code above.
$key='p'; $like="%$key%";
$sql="SELECT contents
FROM myTable
WHERE contents like ?";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
The code above is the original code of mine.
The code below is one of my trials for applying your code which produces Fatal Error
$key='p'; $like="%$key%";
$sql="SELECT contents
FROM LOCATE($key, contents)+1) FROM myTable
WHERE contents like ?";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
The trial below also produces Fatal error.
$sql="SELECT LOCATE($key, contents)+1) as afterKey FROM myTable
WHERE contents like ?";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
The code below produces fatal error : Uncaught PDOException:
$sql="SELECT SUBSTRING(contents FROM LOCATE($key, contents FROM myTable) WHERE contents like ?) as afterKey FROM myTable
WHERE contents like ?";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
$key='p'; $like="%$key%";
$sql="SELECT SUBSTRING(contents, LOCATE($key, contents), 5) as afterKey FROM myTable
WHERE contents like ?";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like]);
SQL gives 3 strings, the 1st one is $beforeKey, the 2nd one is $key, and the 3rd one is afterKey.
I am thinking another question.
Can I get, I am thingking, 1 string which has 5 characters beforeKey, $key, and 5 characters afterKey as a whole from SQL ?
(id) title contents
(1) Asia This is a continent
(2) China This country is big in population
(3) France This country is in Europe and Paris is its capital
(4) Mongolia Mongol is country
(5) Korea Korea
(6) Germany Germany is in Europe and Berlin is its capital
I have myTable like the above.
I made the code below.
$key1='a'; $key2='p'; $like1="%$key1%"; $like2="%$key2%";
$sql="SELECT id, title,
CONCAT(right(SUBSTRING_INDEX(contents, '$key1', 1), 5), '$key1', SUBSTRING(contents, LOCATE('$key1', contents)+1, 5) ) as key1String,
CONCAT(right(SUBSTRING_INDEX(contents, '$key1', 1), 5), '$key1', SUBSTRING(contents, LOCATE('$key1', contents)+1, 5) ) as key1String,ONCAT(right(SUBSTRING_INDEX(contents, '$key1', 1), 5), '$key1', SUBSTRING(contents, LOCATE('$key1', contents)+1, 5) ) as key1String,
FROM myTable
WHERE contents like ? AND contents like ? ORDER BY id";
$searchQ=$dbc-> prepare ($sql);
$searchQ->execute([$like1, $like2]);
The code above produces the result below.
The result above is fine.
This time I like to get one string only which comes first between key1String and key2String.
my target result is like the following.
I can do, I think, it in PHP.
I like to do it in SQL
so content has to contain both $key1 and $key2 because of the LIKEs
then $key1String and $key2String concatenate the 5 characters to the left and 5 characters to the right of both $key1 and $key2
what happens if $key1 or $key2 is more than one character? because then the 5 characters following, where you have LOCATE()+1, will actually be the first 5 characters after the first character of $key1 or $key2
or what happens if $key1 or $key2 occurs closer than 5 characters to the beginning or the end of content? do the SUBSTRINGs work in that situation?
once you have successfully tested your string formulae to cover all those situations, then we can move on to your real question –
my understanding is, you want to pick between $key1String and $key2String and pick the one that occurs first when reading from left to right
if this is correct, you can use the LOCATE function in a CASE expression
CASE WHEN LOCATE(... $key1 ...) > LOCATE(... $key2 ...)
THEN something something key1String
ELSE something something key2String END