where using MATCH()...AGAINST()
where using MATCH()...AGAINST()
I have a database table with a column named TEXTCOL of type TEXT with a FULLTEXT KEY. To get something like a fulltext search on this column I have the following code fragment
$p = $_POST;
if ( isset( $p["search"]) && ( $p["search"]["value"] !== "" )){
$word_arr = preg_split( '/ +/', trim( $p["search"]["value"] ));
foreach( $word_arr as $word ){
$editor -> where( 'TEXTCOL', '%' . $word . '%', 'LIKE' );
}
}
$editor -> process( $p );
$editor -> json();
which works for me. But I know this is bad code since the fulltext index isn't used. Since my table has about 500.000 row entries, the response times are very long. I use serverside processing.
So I like to find some alternative code which uses the database fulltext index with the SQL MATCH()...AGAINST() syntax. Can you please help me.
This question has an accepted answers - jump to answer
Answers
would be the way to do it. The
bind
method is used to use user input without exposing the possibility of an SQL injection attack.Allan
Thanks. It seems that I didn't understand everything. I now have the following code:
but this gives the error message "Invalid JSON response..."
What is the response from the server? Likely it will include an error message.
Allan
On one thing I just spotted, don't use
AGAINST
in the binding. I see I missed that part in my comment above - I'll edit that just now.Allan
Sorry, I had an error in my PHP code which lead to invalid JSON. I corrected that. I also fiddled with " and ' in the strings, but that didn't help. My new code snippet
now leads to this SQL error message:
The
+m
is the first character of my search string. So the correct SQL fragment should bebut whatever I do I am not able to have the
+m
in correct parenthes.Did you try it with
AGAINST
not in the binding like I suggested?That, I think should do it.
Allan
Great! Thanks very much.