• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP & mySQL: Search

Christopher

New Member
I want to know how to search a database-you know like the traditional old search box, then to search the database and return the results. I don't know how, but I think it is something like this:

PHP:
<?php
if($submit)
{
  $query = mysql_query("SELECT * FROM content WHERE title LIKE'$search'"));

  $count = mysql_num_rum($query);

  if($count <= 0)
    print "<br><br><center><b>No results found for the query $search</center><br><br>";

  else
  {
    while($result = mysql_fetch_array($query))
    {
      print "<br><br>";
      print "Title: <a href=\"page.php?id=$result[id]\">$result[title]</a><br>";
      print "<br>$result[summary]";
    }
  }
}

else
{
  ?>
  <form action="<? $PHP_SELF ?>" method="post">
  Search: <input type="text" name="search"><br>
  <input type="submit" name="submit" value="Search!">
  </form>
  <?
}
?>

So would that work? I think my query is error prone... And, how would you "search" for two different things? "WHERE title LIKE '$submit' OR summary LIKE '$submit'"?

Thanx.
 
Thank you, I suppose % is a wildcard?

Anyway, to do more then two 'WHERE's, would you use 'OR' ? I don't even know if it exists, but it seems logical, like every thing else in mySQL.

And, is there a way to (easy?) sort the results by most matching?
 
Yes, % is a wildcard, but don't use it unless you need to, it will slow the search. To have more than one term, combine them with ORs or ANDs, i.e. "WHERE one LIKE 'two' AND three LIKE 'four'". For sorting, unless you want to makeup your own process, you could do a fulltext search and it sorts it on its own.
 
Back
Top