0

The following code is intended to use levenshtein() to compare a user-input word to the values in a column of a MySQL table:

    $searched=$_POST['searched'];

$sql = "SELECT * FROM `word_list`;";

$result = mysqli_query($conn,$sql);

while($row=mysqli_fetch_assoc($result))
    $title = $row['word'];
    $sound = levenshtein($searched, $title);
    if ($sound < 4) {
    echo $title;
}

?>

My confusion stems from the mechanics of actually looping in the values of the "word" column of the table as a variable for the second string int he levenshtein function.

Ultimately, I'd like to loop the values from that column into the $title variable, and echo the values that produce a levenshtein distance less than 4, but I cannot seem to return any output.

1
  • I don't understand your question. while or for doesn't really matter. Use break; if you want to exit the while prematurely. Commented Jun 2, 2013 at 22:58

1 Answer 1

1

Using a while loop is correct in your example. But you are mixing the mysql and the mysqli extension:

$result = mysqli_query($conn,$sql);
...
while($row=mysql_fetch_assoc($result))

You'll have to use mysqli_fetch_assoc() instead.


Also you are missing a closing } at the end of the while loop. The complete example should look like this:

$searched = $_POST['searched'];
$sql = "SELECT * FROM `word_list`;";
$result = mysqli_query($conn, $sql);

while($row = mysqli_fetch_assoc($result))
    $title = $row['word'];
    $sound = levenshtein($searched, $title);
    if ($sound < 4) {
        echo $title;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you; this clarified things a bit, but now I'm receiving a 500 error, so at least now I have some idea of where to go with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.