Table Data is not showing with Pagination PHP

2019-08-21 10:32发布

问题:

Continuation question from Here with pagination.

The basic code for displaying data in the table from mysql.

    $stmt = $mysqli->prepare('SELECT accountno,accountname,cname,revenue,status FROM ***** WHERE user_id = ? '); 
    $stmt->bind_param('i', $user_id);  
   $stmt->bind_result($accountno ,$accountname, $cname, $revenue,$status);
    $stmt->execute();    // Execute the prepared query.
    echo "<table border='1'>
        <tr>
        <th>Account No</th>
        <th>Account Name</th>
        <th>Company Name</th>
        <th>Revenue</th>
        <th>Status</th>
        </tr>";

        while($row = $stmt->fetch()) {

        echo "<tr>";
        echo "<td>" . $row['accountno'] . "</td>";
        echo "<td>" . $row['accountname'] . "</td>";
        echo "<td>" . $row['cname'] . "</td>";
        echo "<td>" . $row['revenue'] . "</td>";
        echo "<td>" . $row['status'] . "</td>";
        echo "</tr>";
        }
        $stmt->close();
        echo "</table>";

Problem 1: With this code, it failed to load data. Showing the table, Showing even the row field but no data.

Problem 2: Adding pagination. Code Example was taken from here Showing Notice of undefining index at this line. Also, want to is this code below is safe and okay in case of performance?

$page=$_REQUEST['p'];

Here is the part for pagination:

 <?php
        //------------
        $page=$_REQUEST['p'];
      $limit=10;
       if($page=='')
    {
    $page=1;
    $start=0;
    }
else
  {
    $start=$limit*($page-1);
   }

    $total=$stmt->num_rows;
    $num_page=ceil($total/$limit);

  function pagination($page,$num_page)
  {
    echo'<ul style="list-style-type:none;">';
    for($i=1;$i<=$num_page;$i++)
 {
       if($i==$page)
    {
    echo'<li style="float:left;padding:5px;">'.$i.'</li>';
    }
    else
     {
      echo'<li style="float:left;padding:5px;"><a href="protected_page.php?
         p='.$i.'">'.$i.'</a></li>';
      }
          }
     echo'</ul>';
     }
     if($num_page>1)
      {
      pagination($page,$num_page);
       }

回答1:

while( $stmt->fetch()) {

        echo "<tr>";
        echo "<td>" . $accountno . "</td>";
        echo "<td>" . $accountname . "</td>";
        echo "<td>" . $cname . "</td>";
        echo "<td>" . $revenue . "</td>";
        echo "<td>" . $status . "</td>";
        echo "</tr>";
        }

you are binding result but using $row to display, it wont, so try this

for pagination

if(isset($_REQUEST['p'])){
//then all the code for pagination
}


标签: php html mysqli