i use the following in one of my scripts (to browse through all the links in the database):
$dbcnx = @mysql_connect($server, $login, $password);
mysql_select_db($db_name);
// The basic SELECT statement
$select = "SELECT DISTINCT link_url, link_name";
$from = " FROM links";
$where = " WHERE link_id > 0 and link_view='y'";
if ($cid != "") { // A category is selected
$where .= " AND link_type_id=$cid";
}
if ($searchtext != "") { // Some search text was specified
$where .= " AND link_name LIKE '%$searchtext%'";
}
$links = mysql_query($select . $from . $where);
if you then still want to add some ordering (like ORDER BY link_name), make sure you add it to the query after the $where statement...