Tell me what is wrong in this code why am getting this error(You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND LIMIT 5,5' at line 1.) when ever I press for next page to see rest of records ?



<?php

include('con.php');


include("resqry.php");


$paging_results = new mysqlpagedresults("SELECT count(*) FROM

users ","page","",5,10,">>>","Previous","Next","<<<" ," | ");

$offset = $paging_results->currentOffset();
$results_per_page = $paging_results->results_per_page;


$where = array();

$agemin=$_POST['agemin'];
$agemax=$_POST['agemax'];

$genders = array('male','female');
$countrystayings = array('uk','usa','italy','France');
$countryorgin = array('uk','usa','italy','France');

if (in_array($countrystaying,$countrystayings))
{
$where[] = "countrystaying= '{$countrystaying}'";
}

if (in_array($countryorgin ,$countryorgins))
{
$where[] = "countryorgin= '{$countrystaying}'";
}

if (in_array($sex,$genders))
{
$where[] = "sex= '{$sex}'";
}

$where[] = 'age BETWEEN '.$agemin.' AND '.$agemax;


$Q=implode(' AND ',$where);
$query = "SELECT * FROM users WHERE $Q LIMIT $offset,$results_per_page";

$result = mysql_query($query) or die(mysql_error());


$counter = 1;

while($row = mysql_fetch_assoc($result)){

echo $row['age'];


print "
<hr>";
$counter++;

}
echo "$first_nav $prev_nav $pages_nav $next_nav $last_nav</p>";

?>