0

Need some help if possible?
I’m trying to add pagination to my code.. The problem I am getting is the pagination script works perfectly well without the JOIN statement and the JOIN statement works when I exclude the pagination script so they both work just not together.

It counts the results correctly with both scripts together just wont show the data.

The question is how do I get them to work together as I get an error:

Any help would be grateful

ini_set('display_errors', 1);
error_reporting(~0);

$Keyword = null;

if(isset($_POST["term"])){
  $Keyword = $_POST["term"];
}

if(isset($_GET["term"])){
  $Keyword = $_GET["term"];
}               

$sql = "SELECT * FROM table1 
  INNER JOIN table2 ON (table1.member_id = members.id) 
  WHERE MATCH (name,location) AGAINST ('%".$Keyword."%' IN NATURAL LANGUAGE MODE)";

$query = mysqli_query($conn,$sql);
$num_rows = mysqli_num_rows($query);
$per_page = 2; 
$page  = 1;

if(isset($_GET["Page"])) { 
  $page = $_GET["Page"];
}                 

$prev_page = $page-1;
$next_page = $page+1;

$row_start = (($per_page*$page)-$per_page);
if($num_rows<=$per_page) {
  $num_pages =1;
} else if(($num_rows % $per_page)==0) {
  $num_pages =($num_rows/$per_page) ;
} else {
  $num_pages =($num_rows/$per_page)+1;
  $num_pages = (int)$num_pages;
}

$row_end = $per_page * $page;
if($row_end > $num_rows) {
  $row_end = $num_rows;
}

$sql .= "SELECT * ORDER BY ID ASC LIMIT $row_start ,$row_end ";

$query = mysqli_query($conn,$sql);
while($result=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
Chris
  • 4,672
  • 13
  • 52
  • 93
phpnewbe
  • 11
  • 2
  • Solved: it was the ORDER BY ID ASC LIMIT $row_start ,$row_end this should read ORDER BY table1.member_id ASC LIMIT $row_start ,$row_end – phpnewbe Nov 04 '17 at 20:20
  • On another note, your script is vulnerable to SQL injection attacks. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – kmoser Nov 04 '17 at 21:09

0 Answers0