Database Searching + Pagination

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Database Searching + Pagination

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Database Searching + Pagination

Post 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.
Image
Post Reply