<?php
function firstItem($array, &$result) {
	if(!is_array($array)) return false;
	foreach($array as $v) $result[] = $v;
}

function searchDatabase($keywords,$fields,$table,$where = '') {
	$result	= array();
	if(strlen($keywords) > 3) {
		$q	= mysql_query("SELECT id FROM ".$table." WHERE MATCH(".implode(',',$fields).") AGAINST('".$keywords."') ".$where) or die(mysql_error());
		while(firstItem(mysql_fetch_row($q), $result));
		
		$split	= explode(' ',$keywords);
		if(count($split) > 1) {
			foreach($split as $v) $result	= array_merge($result,searchDatabase($v,$fields,$table,$where));
		}
		
	} else {
		$q	= mysql_query("SELECT id FROM ".$table." WHERE ".implode(" LIKE '%".$keywords."%' OR ",$fields)." LIKE '%".$keywords."%' ".$where) or die(mysql_error());
		 while(firstItem(mysql_fetch_row($q), $result));
	}
	return array_unique($result);
}
?>