For example mysql table like
TextColumn
------------------------
Full Metal Dog Guard for Passat Estate protecting rear seats and above
User types some text. I want to check if in mysql exists similar text to the typed text
$user_input = 'Full';
query like this
SELECT TextColumn FROM table WHERE MATCH(TextColumn) AGAINST(? IN BOOLEAN MODE) LIMIT 1
$stmt_for_duplicate->execute( array( $user_input ) );
$duplicate_data = $stmt_for_duplicate->fetchAll(PDO::FETCH_ASSOC);
And get
Array
(
[0] => Array
(
[TextColumn] => Full Metal Dog Guard for Passat Estate protecting rear seats and above
)
)
So only one word ('Full') is the same as in TextColumn
and i get that result is similar
Seems i incorrectly use MATCH AGAINST
.
Please, advice how to use correctly? May be I can add some percentage somehow? Or may be exists another simple and fast solution?
Another way but it is not effective (change one letter and get "as if not similar"). So better to use MATCH ... AGAINST
but AGAINST
must contain some sentence
I do not know how effective is such solution, but here it is (please, advice if can improve something).
So I have user input. I create array from certain number of first words
$array_from_input = explode( " ", ( substr( (trim($_POST['user_input'])),0,150 ) ) );
Then create query and data
$part_of_sql_check_similar = '';
foreach( $array_from_input as $i_array_from_input => $v_array_from_input ) {
if( $i_array_from_input == 0 ){
$part_of_sql_check_similar .= 'TextColumn LIKE ? ';
}
else{
$part_of_sql_check_similar .= 'AND TextColumn LIKE ? ';
}
$data_check_similar[] = '%'. trim($v_array_from_input). '%';
}
And then select
try {
$stmt_for_duplicate = $db->prepare( '
SELECT
Id FROM table_name
WHERE '.
$part_of_sql_check_similar.
'LIMIT 1
;' );
$stmt_for_duplicate->execute( $data_check_similar );
$duplicate_data = $stmt_for_duplicate->fetchAll(PDO::FETCH_ASSOC);
}
In such way i check if one particular row contains all the words from user's input. All works, but how about performance? Seems slow?