分页不在php查询中工作

时间:2013-10-03 01:29:23

标签: php mysqli pagination

嗨我正在尝试为mysql查询创建分页,但它根本不起作用,有人可以告诉我我的错误在哪里吗?到目前为止,这是我的代码。

<?php     

$con=mysqli_connect("localhost","database","password","table");
// Check connection
if (mysqli_connect_errno())
{
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

if (!$result = mysqli_query($con,"SELECT * FROM table_name"))
{
    die("Error: " . mysqli_error($con));
}
$per_page =5;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);

   if(!isset($_GET['page']) || 
   $_GET['page']=="") {
      $page="1";
} else {
  $page=$_GET['page'];
}    $start    = ($page - 1) * $per_page;
    $result = mysqli_query($con,"SELECT * FROM cursos LIMIT $start,$per_page");

这是我在我的SQL查询列出结果的部分

?>

// show results from my sql query 

<?php

while($row = mysqli_fetch_array($result))
{   $id = $row['id'];
?>


// code for next page 

最后这是页面底部的分页链接
    

//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
  <li id="<?php echo $i;?>"><a href="cursos.php?c=<?php echo $c;?>&page=<?php echo $i;?>"><?php echo $i;?></a></li>
<?php  } ?>

在Deepak的建议之后,我现在才开始

错误的最终代码//Show page它会生成虚假链接 注意:未定义的变量: D:\ xampp \ htdocs \ cedecap \ admin \ cursos.php 191 的颜色< 安培&;页= 1" →1

代码//显示页面链接没有显示任何内容。现在页面上的页面正在运行

5 个答案:

答案 0 :(得分:3)

更改以下行

$result = $result." LIMIT $start,$per_page";

$result = mysqli_query($con,"SELECT * FROM table_name LIMIT $start,$per_page");

修改

在行$query_count=mysqli_query($con, $result);中,您将mysql_query()的结果再次传递给同一个函数。您可以跳过该行并执行以下操作:

//$query_count=mysqli_query($con, $result); this line is deleted

$per_page =5;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);

还要进行以下更改以消除index错误:

if(!isset($_GET['page']) || $_GET['page']=="") {
  $page="1";
} else {
  $page=$_GET['page'];
}

对于未定义的变量$c错误,在设置$page变量后添加以下代码:

if(!isset($_GET['c']) || $_GET['c']=="") {
  $c="some value";
} else {
  $c=$_GET['c'];
}

答案 1 :(得分:1)

许多错误,一开始

mysql_num_rows($query_count);

应该是

mysqli_num_rows($query_count);

答案 2 :(得分:1)

您正在使用mysql_ *命令而不是mysqli _ *

您在以下几个地方使用它们:

$query_count=mysql_query($result);

$count = mysql_num_rows($query_count);

将这些转换为:

 $query_count=mysqli_query($con, $result);

 $count = mysqli_num_rows($query_count);

除了@deepak在他的回答中指出的问题。

答案 3 :(得分:0)

看起来您将“LIMIT $ start,$ per_page”附加到结果对象而不是基本SQL语句。尝试将查询分配到顶部变量,然后附加到该变量,如下所示:

$sql = "SELECT * FROM table_name";
if (!$result = mysqli_query($con,$sql))
{
    die("Error: " . mysqli_error($con));
}

$query_count=mysqli_query($con,$result);

$per_page =5;//define how many games for a page
$count = mysqli_num_rows($query_count);
$pages = ceil($count/$per_page);

if($_GET['page']==""){
    $page="1";
}else{
    $page=$_GET['page'];
}
$start    = ($page - 1) * $per_page;
$result     = mysqli_query($con,$sql." LIMIT $start,$per_page");

答案 4 :(得分:0)

也许这一行错了:

$con=mysqli_connect("localhost","database","password","table");

应该是这样的:

$link = mysqli_connect("myhost","myuser","mypassw","mybd");

尝试:

$link = mysqli_connect("localhost","isuposseyouruserisroot","youdatabasepass","nameofyourdatabase") or die("Error " . mysqli_error($link));