Levenshtein: MySQL + PHP

Levenshtein: MySQL + PHP

You need a levenshtein function in MySQL and query like

$word = mysql_real_escape_string($word);
mysql_qery("SELECT `term` FROM `words` WHERE levenshtein('$word', `term`) BETWEEN 0 AND 4");

Using levenshtein in MySQL search for one result

You would do:

SELECT p.*
FROM people p
ORDER BY levenshtein('$message', msg1) ASC
LIMIT 1;

If you want a threshold (to limit the number of rows for sorting, then use a WHERE clause. Otherwise, you just need ORDER BY.

PHP Levenshtein on Query Result

Your logic for fetching the games (words) from your database is correct, but you need to remove...

$words = $row_GID['game'];

and pass in the $row_GID variable to your loop.

Then in your foreach loop...

foreach ($row_GID as $row) {
$word = $row['game'];
//proceed as normal
}

MYSQL LIMIT LEVENSHTEIN

You can use a having clause:

SELECT title, LEVENSHTEIN(title, 'A MATCHING ') AS distance
FROM jb_jobs
WHERE description LIKE '%lorem%'
HAVING distance <= 4
ORDER BY distance DESC;

The use of HAVING without a GROUP BY in this fashion is a MySQL extension. In other databases, you would typically use a subquery or CTE. However, subqueries add additional overhead (because MySQL materializes them) and MySQL does not support CTEs.

PHP MySQL - Levenshtein alternative on decimal

There are numerous methods to calculate geographic distance between sets of points. Some are accurate where others are fast. Which method you choose is all depends on your requirements. As there are many answered questions here at Stack Overflow which have specific answers:

https://stackoverflow.com/search?tab=votes&q=distance%20mysql

Return result of an mysql function

You need to use an alias the function return

SELECT levenshtein('butt', 'but') as levenshtein FROM 

then it can be accessed from the levenshtein index.

$row['levenshtein']

Also note:

In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.

So if you didn't want the alias you could do:

$row[1]

this is less readable/maintainable to me though.

When in doubt about what an array contains you can check it with print_r or var_dump. Those will give you the indices and the values the array has.



Related Topics



Leave a reply



Submit