3

I am trying to perform a simple text search.

However, if I use mysqli_real_escape_string it makes the program buggy.

I have a Fulltext column title, but the functionality does not work in phpMyadmin after I do :

PhpMyadmin

 - **id|title** 
 - 1|cool boy
 - 2|cool bottle
 - 3|cool guy
 - 4|hot man

Here is the rest of my php files :

file1.php

   //from a form

   $str = $_POST['q'];
   $str=mysqli_real_escape_string($con,$str);
   mysqli_query($con,"insert into tbl(title)values($str)");

file2.php

$str = $_GET['q'];

$data = mysqli_query($con,"select * from tbl where match(title)against('$str')");
while($row = mysqli_fetch_assoc($data)){
   //do stuff . but yields nothing
}

How can I properly perform a Fulltext search of my title column?

MFerguson
  • 1,739
  • 9
  • 17
  • 30

2 Answers2

1

Try this

  select * from tbl where title LIKE '%cool%'

EDIT: if you wanna do fulltext search then change this

  against('cool')

to

 against('cool' IN BOOLEAN MODE)

In MySQL there are three types of full-text searches:

  • boolean search
  • natural language search (used by default)
  • query expansion search

From MySQL manual entry:

DEMO HERE

EDIT2:

do in file1.php

   session_start(); //this will be in very top of your file
   $str = $_POST['q'];
   $_SESSION['str'] = $str;
   $str=mysqli_real_escape_string($con,$str);
   mysqli_query($con,"insert into tbl(title)values($str)");

in file2.php

   session_start(); //this will be in very top of your file
   $str = $_SESSION['str'];
   $data = mysqli_query(.........
echo_Me
  • 37,078
  • 5
  • 58
  • 78
1
select * from tbl where title LIKE 'cool%'

add only one % after your string/variable

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62