Return mysql 函数的结果

Return result of an mysql function

我已经在 MYSQL 中安装了 this Levenshtein function。厂商建议使用这个:

select levenshtein('butt', 'but') from test;
select levenshtein_ratio('butt', 'but') from test;

我想计算 $search 和数据库中每个 "name" 条目之间的 Levenshtein 比率,然后在 PHP 中回显它。

我怎样才能做到这一点?提前致谢

$search = "Paul"; // The string I want compared to each DB entry

$query = "SELECT name, ??? FROM names"; // What's the MYSQL I need to use?

$response = @mysqli_query($dbc, $query);

while($row = mysqli_fetch_array($response)){

  echo "Levenshtein Ratio: ".$row['???'];
  echo "<br>";

}

您需要为函数使用别名return

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

然后可以从levenshtein索引访问。

$row['levenshtein']

另请注意:

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.

因此,如果您不想要别名,您可以这样做:

$row[1]

虽然这对我来说 readable/maintainable 少。

如果对数组包含的内容有疑问,可以使用 print_rvar_dump 进行检查。这些将为您提供数组的索引和值。

这个怎么样?

<?php 

$search = "Paul"; // The string I want compared to each DB entry
$query = "SELECT name, levenshtein(name, '".mysqli_real_escape_string ($dbc, $search)."') AS result FROM names";

$result = @mysqli_query($dbc, $query);

while($row = mysqli_fetch_array($result)){

  echo "Levenshtein Ratio for ".$row['name'].' : '.$row['result']."<br />";

}