Page 1 of 1

Database Searching + Pagination

Posted: Sat Aug 11, 2012 3:00 pm
by wrichards8
Hey. I am attempting to conbine database searching with pagination but am having problems. The function I have writen looks like this
function searchDB($condition, $value, $page, $per_page)
{
	$condition 				= htmlentities(trim(mysql_real_escape_string($condition)));
	$value 						= htmlentities(trim(mysql_real_escape_string($value)));
	$keywords 				= preg_split("#/[\s]+/#", $value);
	$keyword_where 	= "AND `{$condition}` LIKE '%". implode("%' OR `{$condition}` LIKE '%", $keywords). "%'";
	$perpage 				= (int)$page;
	$startpage 				= (int)($page - 1) * $page;
	$results 					= array();
	$query 					= mysql_query("SELECT `dvdid`, `title`, `genre`,`rating`,`releaseyear`,`dvdtype`,`dateadded`,`disctype` FROM `audio_described_dvd` WHERE `approved` = 1 {$keyword_where} LIMIT {$startpage}, {$perpage}");
	while($row = mysql_fetch_assoc($query))
	{
		$results[] = $row;
	}
	return $results;
When I try and execute this in my script by doing
$page = $_GET["page"];
	if(empty($page))
	{
		$page = 1;
	}
	$searchresults = searchDB($_POST["searchby"], $_POST["searchterm"], $page, 5);
it always returns an empty array. I'm not sure what the problem is

Re: Database Searching + Pagination

Posted: Sun Aug 12, 2012 12:39 am
by jacek
I would try the query in phpmyadmin to see if you are getting an error or just no results, you might need to add an echo to get the created SQL.