douchu4048 2013-02-05 16:53
浏览 39

限制post sql结果

Im building a search engine from scratch.

  • It scans many different tables in multiple databases.
  • It searches through only the specified columns.
  • The search words are explode()'ed case of multiple words search
  • It iterates with a foreach through the tables.

I'm having trouble structuring the code base with LIMIT. I want it to show only DISTINCT.

For example if I search Joe Schmoe my code will search for first = joe and then for last = schmoe, and it will display a result for each search. But I want it to display only one result for the two searches.

<?php

echo "<h1>Search Results</h1>";

echo "<table style='width: 100%;'>";

$a = $_GET["q"];

$b = explode(" ", $a);

include 'db_connect3.php';

mysql_select_db("db440035579", $con);

foreach($b as $c){
$sql="SELECT * FROM users WHERE first LIKE '%$c%' || last LIKE '%$c%'";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
  echo "<a href='applications.php?act=search&uid=$row[7]'>$row[5], $row[4]</a>";
  }
}


mysql_close($con);
?>
  • 写回答

1条回答 默认 最新

  • dtvhqlc57127 2013-03-19 14:32
    关注

    First of all http://bobby-tables.com/

    Secondly, plugging in PDO takes only a few lines of code.

    Thirdly, FULL TEXT SEARCH is much better.

    And finally:

    Use a array microcaching. I assume $row[0] is the user.id. That way you only get one item per ID

    $cache = array();    
    foreach($b as $c){
        $sql="SELECT * FROM users WHERE first LIKE '%$c%' || last LIKE '%$c%'";
    
        $result = mysql_query($sql);
    
        while($row = mysql_fetch_array($result)){
            $cache[$row[0]] = $row;
        }
    }
    foreach($cache as $row){
        echo "<a href='applications.php?act=search&uid=$row[7]'>$row[5], $row[4]</a>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了