-3

I am stuck with the query result and need help plz.

My sample column How are you what is your name where are you from whats the age of the youth

Now this is my column in Table and I query the below

  $search='you';
  $s=mysqli_query($con,"Select * from tblxyz where column1 is LIKE '%{$key}%'");

I am getting the result of all the 4 rows as there is "you" in all, But I would like to get the result of the exact word which SQL searched, so the result from the 4th row would be "youth". How to get those words which SQL found based on my query.

Thanks in Advance

Sree Aka
  • 29
  • 1
  • 5

3 Answers3

2

try this query.

  $search='you';
     $sql=" SELECT * FROM tblxyz WHERE column1 like '%".$search."%'";
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • Thanks buddy, But this also gives me the whole column as result, But I just need the word as result from which it has matched. Thanks – Sree Aka Oct 17 '18 at 05:21
  • instead of WHERE column1 like '%".$search."%' use WHERE column 1=$search – PHP Geek Oct 17 '18 at 05:41
  • 1
    While your code may correct as answer But Elaborating what your code does, It can improve the quality of your answer. Checkout the Article : [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – LuFFy Oct 17 '18 at 06:22
0

to get the exact word don't use %

$search='you';
$s=mysqli_query($con,"Select * from tblxyz where column1 is LIKE '{$search}'");
Exterminator
  • 1,221
  • 7
  • 14
  • Thanks for quick response, but my question is little different. With the query above I get the result as the entire column (All 4 of them), But the result I need is 1. you, 2. you, 3. you and 4. youth, This is what I am looking for, Thanks in advance – Sree Aka Oct 17 '18 at 05:14
  • so you want result from the column which contains word `you` in the string. It doesn't matter if it has any letter preceding and succeeding it. So if word youth is there it should show that in result. – Exterminator Oct 17 '18 at 05:28
  • Ok so now the result is showing from all the 4 rows, The result i get is 1) how are you, 2) what is your name, 3) where are you from and 4) whats the age of the youth. I am getting all the 4 which is fine, But I want the result to be 1) you, 2) your, 3) you, 4) youth. Only these words. Thanks – Sree Aka Oct 17 '18 at 05:38
0

Let's say you are looking for the word you and you want to select only data with the word you. In this case, you might want to use REGEXP.

"SELECT * FROM table WHERE tname RLIKE "[[:<:]]you[[:>:]]"

The above can be used for exact word matching.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23