首页 > 解决方案 > MySQL查询结果后如何显示下5条记录或行?

问题描述

我在表中有一个包含类似单词的列

  1. 抗菌
  2. 花生
  3. 弃权
  4. 疏离
  5. 阿布内特
  6. 阿普蒂斯顿

因此,当我查询单词“Abacterial”时,它会显示“Abacterial”的结果,但我还想显示接下来的 5 个单词。

我使用这个 MySQL 查询代码:mysqli_query($conn, "SELECT * FROM medi_words WHERE word LIKE '%$query%'");

标签: phpmysqli

解决方案


直接来自文档

$query = "SELECT * FROM medi_words WHERE word LIKE '%$query%' LIMIT 5";
if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["word"]);
    }

    /* free result set */
    mysqli_free_result($result);
}

推荐阅读