Here is the code:
Code: Select all
function searchLinks($terms)
{
$result = mysql_query("SELECT codeid, link FROM links WHERE link LIKE '%$terms%' ORDER BY noinset Desc")
or die(mysql_error());
$_SESSION['totalqueries']++;
$num_rows = mysql_num_rows($result);
$_SESSION['results'] += $num_rows;
for($i = 0; $i < $num_rows; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['codeid'];
$result2 = mysql_query("SELECT title, getcode FROM codes WHERE id='$id'")
or die(mysql_error());
$row2 = mysql_fetch_array($result2);
$_SESSION['totalqueries']++;
$link = str_replace($terms, "<b>" . $terms . "</b>", $row['link']);
echo "<tr class=\"searchresultsresults\"><td colspan=\"3\" width=\"50%\"><a href=\"index.php?page=get&code=" . $row2['getcode'] . "\">$link</a></td></tr>";
}
if($num_rows == "0")
{
echo "<tr><td colspan=\"3\"><div align=\"center\">No results found</div></td></tr>";
}
} // end searchLinksExample:
user searches for bob, the links table is searched for "bob" (+1 query), if a result is found the codeid of that link is matched up with the id of the title in the codes table (+1 query).
However there may be 50 to 100+ links associated with a single codeid that links to a id in the codes table, so for each found link it is also re-querying the codes table to get the same information.
Any way to slim down the queries so it doesn't take 900 queries to display 900 search results?