在MySQL中使用关键字LIMIT

时间:2013-11-24 09:33:13

标签: php mysql sql

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data_wis1") or die(mysql_error());
  if(isset($_GET["id"])) {
    $id = $_GET["id"];
    $sql = "DELETE FROM info WHERE ID = '".$id."'";
    mysql_query($sql) or die(mysql_error());
  }
  if(isset($_GET["page"])) {
    $page = $_GET["page"];
  }
  else {
    $page = 1;
  }
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM info LIMIT 0, 1";
$query = mysql_query($sql) or die(mysql_error());
?>
<html>
  <table>
    <?php
      for($i = 0; $i <mysql_num_rows($query); $i++) {
        $id = mysql_result($query, $i, "ID");
    $caseStatus = mysql_result($query, $i, "Case_Status");

    echo "<tr>";
          echo "<td>".$id."</td>";
      echo "<td>".$caseStatus."</td>";
      echo "<td><a href='del.php?id=".$id."'><input type='button' value='Delete'></a></td>";
    echo "</tr>";
      }
      ?>
  </table>
  <?php
    $sql = "SELECT COUNT(ID) FROM info";
    $query = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_row($query);
    $total_records = $row[0];
    $total_pages = ceil($total_records / 1);

    for ($i=1; $i<=$total_pages; $i++) {
      echo "<a href='del.php?page=".$i."'>".$i."</a> ";
    }
  ?>
</html>

我的问题是每当我点击第2,3,4页......显示都不会改变。第2,3,4页显示第1页。应该是这样的。例如,第1页应显示ID = 1且案例状态=打开。第2页应显示ID = 2且案例状态=关闭等。

1 个答案:

答案 0 :(得分:0)

你从未在任何地方使用过$ start_from。我认为$sql = "SELECT * FROM info LIMIT '".$start_from."', 1";就是答案