Count total rows from link

On my site i have apage that shows all the entrys in the db and each one has a link that goes to another page called details.php and it shows a year like 1988 and i have setup a link to go to another page to display all entries in that year. and that works but i have pagination and i need to get the result of the total rows but do not know how to count the variable that has the year in it.

This is the code that gets the what i clicked.

<?php

if(isset($_GET['id'])){

$id = mysqli_real_escape_string($db, $_GET['id']);

// make sql

$sql = "SELECT * FROM games WHERE id= $id";

// get the query result

$result = mysqli_query($db, $sql);

// fetch result in array format

$game = mysqli_fetch_assoc($result);

mysqli_free_result($result);

}

?>

And here is the code that calculates the pagination

// build pagination links: prev, 1, range around current page, total_pages, next

// number of +/- links around the current page number

$range = 3;

// number of items per page

$limit = 16;

// get total number of rows using a SELECT COUNT(*) ... query

$total_rows = 103;

// calculate total number of pages

$total_pages = ceil($total_rows / $limit);

// get current page, default to page 1

$page = $_GET['page'] ?? 1;

// limit page to be between 1 and $total_pages

$page = max(1,min($total_pages,$page));

$initial_page = ($page-1) * $limit;

// produce array of pagination numbers: 1, range around current page, total_pages, without duplicates, between 1 and total_pages

$links = array_filter(array_unique(array_merge([1],range($page-$range, $page+$range),[$total_pages])),

function ($val) use ($total_pages) { return $val >= 1 && $val <= $total_pages; });

// build pagination links

$pagination_links = '';

// get a copy of any existing get parameters

$get = $_GET;

How can i get to count the total rows every time i click a year ?

I have solved this problem

The above is NOT secure. The id is a numerical value, not a string value, and mysqli_real_escape_string won’t protect against sql injection in the value.

You should use a prepared query, since it provides protection for all data types.

Sponsor our Newsletter | Privacy Policy | Terms of Service