douchi5822 2013-06-22 20:24
浏览 19
已采纳

将获取的数据从db限制为HTML中的列表

I'm working on a autosuggest field and it works already. The only thing I'm trying to do now, is to limit the output of data in the list below the search field. Can someone help me how to do it? I tried a few ways already, but ending up with errors. So what I'm trying is to limit the amount of results which get pulled out of the database. I tried doing this in php (I think it's better performance wise, isn't it?). Here's the code that works fine already:

<?php
require_once 'connect.php';

if (isset($_POST['search_term']) == true && empty ($_POST['search_term']) == false) {
    $search_term = mysql_real_escape_string ($_POST['search_term']);
    $query = mysql_query ("SELECT `word` FROM `datalist` WHERE `word` LIKE '$search_term%'");
    while (($row = mysql_fetch_assoc($query)) !==false) {
        echo '<li>', $row['word'], '</li>';
    }   
}
?>

Since I'm not an expert, I would be happy for some help to learn more...

  • 写回答

2条回答 默认 最新

  • dongping8572 2013-06-22 20:28
    关注

    The easiest approach would be to update your sql query and add a limit clause. So if you maybe want only the first 10 results, do it like this:

    SELECT `word` 
    FROM `datalist` 
    WHERE `word` 
    LIKE '$search_term%'
    LIMIT 10;
    

    You can do that in php without modifying your query, too, but I found it easiest if you just pull from the database what you need.

    By the way, you're using the old mysql connector. This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误