I'm trying to do a look up where keyword = keyword.. but I would like to force at least 2 results to always be returned.. so there may be only be 1 or maybe 0 matches, if that is the case, it should just return any 2...
Is this posible on the sql end?
Forcing a set number of results
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
$result = mysql_query("SELECT count(*) FROM `table` WHERE `foo` = '$bar'") or die(mysql_error());
if(mysql_result($result,0) < 2){
$result = mysql_query("SELECT `something` FROM `table` ORDER BY RAND() LIMIT 2");
} else {
$result = mysql_query("SELECT `something` FROM `table` WHERE `foo` = '$bar' LIMIT 2");
}Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
THANK YOU!scottayy wrote:Code: Select all
$result = mysql_query("SELECT count(*) FROM `table` WHERE `foo` = '$bar'") or die(mysql_error()); if(mysql_result($result,0) < 2){ $result = mysql_query("SELECT `something` FROM `table` ORDER BY RAND() LIMIT 2"); } else { $result = mysql_query("SELECT `something` FROM `table` WHERE `foo` = '$bar' LIMIT 2"); }