PHP MySQL搜索脚本 - 只匹配一些字母

时间:2018-06-12 08:31:02

标签: php mysql search

在我的网站上有搜索功能。问题是: 如果我写单词Litmanova它没有找到任何东西,因为在mysql中有单词litmanovej,litman等。

如果我将litmanova放入搜索输入中,有没有办法找到例如结果litmanovej? 谢谢。

function search() {
  echo "<div class='col-md-12' style='margin-top:20px;'>";
  echo "<div id='custom-search-input'>";
  echo "<form action='vyhladavanie' method='GET'>";
  echo "<div class='input-group col-md-12'>";
  echo "<input type='text' class='form-control input-lg' name='query' placeholder='Napíšte slovo, ktoré hľadáte...' />";
  echo "<span class='input-group-btn'>";
  echo "<button class='btn btn-info btn-lg' type='submit' style='background:#cf585b;border:1px #cf585b solid;'>";
  echo "<i class='glyphicon glyphicon-search'></i>";
  echo "</button>";
  echo "</span>";
  echo "</div>";
  echo "</form>";
  echo "</div>";
  echo "</div>";
}


function showVideoResults() {
  global $conn;
  $query = $_GET['query'];
  $query = "SELECT * FROM videos WHERE video_title LIKE '%".$query."%' OR video_description LIKE '%".$query."%' ORDER by video_date DESC";
  $result = mysqli_query($conn, $query);

  if (!$result) {
    die("Problém" . mysqli_error($conn));
  }

  while ($row = mysqli_fetch_array($result)) {
      $video_id = $row['video_id'];
      $video_title = $row['video_title'];
      $video_date = $row['video_date'];
      $video_description = $row['video_description'];
      $video_link = $row['video_link'];
      $video_category = $row['video_category'];
      $video_url = $row['video_url'];

      echo "<div class='col-md-4'>";
      echo "<a href='video/$video_id-$video_url'>";
      echo "<h3 class='meta_title'>$video_title</h3></a>";

      echo "</div>";
  }
}

2 个答案:

答案 0 :(得分:0)

我担心没有&#34;简单&#34;这样做的方法,但你可以使用MATCH AGAINST

有一些不错的例子here

答案 1 :(得分:0)

您不必对代码进行任何更改。您需要更改数据库表列的定义

您需要将COLLATE UTF8_GENERAL_CI添加到列的定义中。

ALTER TABLE videos 
 MODIFY COLUMN video_title VARCHAR(…) CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI;