The pagination code below works well in mysql_deprecated as follows
When i tried to move it to PDO for sql injection protection it does not fetch any records from database below is the codeCode:<?php require('database.php'); if (isset($_GET['chmsc'])) { $chmsc=$_GET['chmsc']; $chmsc=strip_tags($chmsc); $chmsc=htmlentities($chmsc, ENT_QUOTES, "UTF-8"); } else $chmsc=1; ?> <?php $per_page=strip_tags(2); $total_pages=htmlentities(ceil($rows/$per_page), ENT_QUOTES, "UTF-8"); echo"page $chmsc of $total_pages<br>"; if($chmsc!=1) { echo "<a href='home.php?chmsc=1'>First </a>"," "; $previous=htmlentities($chmsc-1, ENT_QUOTES, "UTF-8"); echo "<a href='home.php?chmsc=$previous'> Previous</a>", " "; } if (($chmsc!=1) && ($chmsc!=$total_pages)) echo "||"; if($chmsc!=$total_pages) { $next=htmlentities($chmsc+ 1, ENT_QUOTES, "UTF-8"); echo "<a href='home.php?chmsc=$next'>Next </a>"," "; echo "<a href='home.php?chmsc=$total_pages'> Last</a>"; } echo "<br/><br/>"; $x=($chmsc-1)*$per_page; $query = "SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE (username = '$user') ORDER BY post_id DESC limit $x, $per_page"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ // continue ?>
I discovered that the problem is in the sql query statementCode:<?php require('database.php'); if (isset($_GET['chmsc'])) { $chmsc=$_GET['chmsc']; $chmsc=strip_tags($chmsc); $chmsc=htmlentities($chmsc, ENT_QUOTES, "UTF-8"); } else $chmsc=1; ?> <?php $per_page=strip_tags(2); $total_pages=htmlentities(ceil($rows/$per_page), ENT_QUOTES, "UTF-8"); echo"page $chmsc of $total_pages<br>"; if($chmsc!=1) { echo "<a href='home.php?chmsc=1'>First </a>"," "; $previous=htmlentities($chmsc-1, ENT_QUOTES, "UTF-8"); echo "<a href='home.php?chmsc=$previous'> Previous</a>", " "; } if (($chmsc!=1) && ($chmsc!=$total_pages)) echo "||"; if($chmsc!=$total_pages) { $next=htmlentities($chmsc+ 1, ENT_QUOTES, "UTF-8"); echo "<a href='home.php?chmsc=$next'>Next </a>"," "; echo "<a href='home.php?chmsc=$total_pages'> Last</a>"; } echo "<br/><br/>"; $x=($chmsc-1)*$per_page; $result = $db->prepare('SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE username = :username ORDER BY post_id DESC limit $x, $per_page'); $result->execute(array( ':username' => $user )); // continue ?>
if i used query statement like thisCode:$result = $db->prepare('SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE username = :username ORDER BY post_id DESC limit $x, $per_page');
it will query the records but it will not pagenate it. when i added the variablesCode:$result = $db->prepare('SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE username = :username ORDER BY post_id DESC ');
$x, $per_page
it will fetch empty page. any help please



Reply With Quote


Bookmarks