限制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);
?>

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>";
}