title: Convert mysqli pagination to prepared statement pagination
body: <p>the current working code with mysqli is as below. I would like to convert it to prepared statements. there is basically three sql query need to convert to prepared statement. the first part is the hardest part for me to figure out the prepared way to get count value which i kinda managed to figure it out eventually.</p>
  
  <pre><code>&lt;?php
  // Script and tutorial written by Adam Khoury @ developphp.com
  // Line by line explanation : youtube.com/watch?v=T2QFNu_mivw
  include_once("storescripts/connect_to_mysqli.php");
  // This first query is just to get the total count of rows
  $sql2 = "SELECT COUNT(id) FROM products ";
  $query2 = mysqli_query($myConnection, $sql2);
  $row = mysqli_fetch_row($query2);
  // Here we have the total row count
  $rows = $row[0];
  // This is the number of results we want displayed per page
  $page_rows = 10;
  // This tells us the page number of our last page
  $last = ceil($rows/$page_rows);
  // This makes sure $last cannot be less than 1
  if($last &lt; 1){
      $last = 1;
  }
  // Establish the $pagenum variable
  $pagenum = 1;
  // Get pagenum from URL vars if it is present, else it is = 1
  if(isset($_GET['pn'])){
      $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
  }
  // This makes sure the page number isn't below 1, or more than our $last page
  if ($pagenum &lt; 1) { 
      $pagenum = 1; 
  } else if ($pagenum &gt; $last) { 
      $pagenum = $last; 
  }
  // This sets the range of rows to query for the chosen $pagenum
  $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
  // This is your query again, it is for grabbing just one page worth of rows by applying $limit
  $sql = "SELECT id,product_name, price FROM products ORDER BY product_name DESC $limit";
  $query = mysqli_query($myConnection, $sql);
  // This shows the user what page they are on, and the total number of pages
  $textline1 = "Products (&lt;b&gt;$rows&lt;/b&gt;)";
  $textline2 = "Page &lt;b&gt;$pagenum&lt;/b&gt; of &lt;b&gt;$last&lt;/b&gt;";
  // Establish the $paginationCtrls variable
  $paginationCtrls = '';
  // If there is more than 1 page worth of results
  if($last != 1){
      /* First we check if we are on page one. If we are then we don't need a link to 
         the previous page or the first page so we do nothing. If we aren't then we
         generate links to the first page, and to the previous page. */
      if ($pagenum &gt; 1) {
          $previous = $pagenum - 1;
          $paginationCtrls .= '&lt;a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'"&gt;Previous&lt;/a&gt; &amp;nbsp; &amp;nbsp; ';
          // Render clickable number links that should appear on the left of the target page number
          for($i = $pagenum-4; $i &lt; $pagenum; $i++){
              if($i &gt; 0){
                  $paginationCtrls .= '&lt;a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'"&gt;'.$i.'&lt;/a&gt; &amp;nbsp; ';
              }
          }
      }
      // Render the target page number, but without it being a link
      $paginationCtrls .= ''.$pagenum.' &amp;nbsp; ';
      // Render clickable number links that should appear on the right of the target page number
      for($i = $pagenum+1; $i &lt;= $last; $i++){
          $paginationCtrls .= '&lt;a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'"&gt;'.$i.'&lt;/a&gt; &amp;nbsp; ';
          if($i &gt;= $pagenum+4){
              break;
          }
      }
      // This does the same as above, only checking if we are on the last page, and then generating the "Next"
      if ($pagenum != $last) {
          $next = $pagenum + 1;
          $paginationCtrls .= ' &amp;nbsp; &amp;nbsp; &lt;a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'"&gt;Next&lt;/a&gt; ';
      }
  }
  
  
  $dynamicList = '';
  while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
      $id = $row["id"];
      $product_name = $row["product_name"];
      $price = $row["price"];
      $dynamicList .= "
                  &lt;li&gt;&lt;div class='product'&gt;
                  &lt;a href='product.php?id=$id' class='info'&gt;
                  &lt;span class='holder'&gt;
                  &lt;img src='inventory_images/$id.jpg' alt='$product_name' /&gt;
                  &lt;span class='book-name'&gt;$product_name&lt;/span&gt;
                  &lt;/a&gt;
                   &lt;a href='product.php?id=$id' class='buy-btn'&gt;RM&lt;span class='price'&gt;$price&lt;/span&gt;&lt;/a&gt;
                  &lt;/div&gt;
                  &lt;/li&gt;
  
                ";
  }
  // Close your database connection
  mysqli_close($myConnection);
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;style type="text/css"&gt;
  body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
  div#pagination_controls{font-size:21px;}
  div#pagination_controls &gt; a{ color:#06F; }
  div#pagination_controls &gt; a:visited{ color:#06F; }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt;
    &lt;h2&gt;&lt;?php echo $textline1; ?&gt; Paged&lt;/h2&gt;
    &lt;p&gt;&lt;?php echo $textline2; ?&gt;&lt;/p&gt;
    &lt;p&gt;&lt;?php echo $dynamicList; ?&gt;&lt;/p&gt;
    &lt;div id="pagination_controls"&gt;&lt;?php echo $paginationCtrls; ?&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>thanks in advance for any assistance</p>