警告:mysqli_free_result()预计只有1个参数,给定2

时间:2015-02-08 20:44:51

标签: php mysqli pagination

我有一些分页数据的代码。我的数据显示,但分页不起作用。我收到以下错误:

  

警告:mysqli_free_result()需要1个参数,2个给定

之前,在我将mysql_result()更改为mysqli_free_result()之前,我的分页使用了 mysql

这是我的代码:

<table class="table table-striped table-hover fill-head">
 <thead>
  <tr>
   <th>Code Poli</th>
   <th>Name Poli</th>
   <th>Floor</th>
   <th style="width: 150px">Action</th>
  </tr>
 </thead>
 <tbody>

<?php 

  $con=mysqli_connect("localhost","root","","klinik");
  $per_page = 2;
  $query = "SELECT COUNT(*) FROM poliknik";
  $page_query = mysqli_query($con,$query);
  $pages = ceil(mysqli_free_result($page_query, 0) / $per_page);
  $page = (isset($_GET['hal'])) ? (int)$_GET['hal'] : 1;
  $start = ($page - 1) * $per_page;         
  $data_kg=mysqli_query($con,"select * from poliknik order by id_poli DESC LIMIT $start,$per_page");
  while ($kg=mysqli_fetch_object($data_kg)){

?>

  <tr>
   <td><?php echo $kg->kode?></td>
   <td><?php echo $kg->name_poli?></td>
   <td><?php echo $kg->lt?></td>
   <td>
   <a class="btn btn-primary btn-sm" href="home.php?p=edit_poliknik&id_poli=<?php echo $kg->id_poli ?>"><i class="fa fa-edit"></i> Edit</a>
   <a class="btn btn-danger btn-sm" href="home.php?p=hapus_poliknik&id_poli=<?php echo $kg->id_poli ?>"><i class="fa fa-trash-o"></i> Delete</a>

   </td>
  </tr>

<?php } ?>

 </tbody>
</table>
<center>
 <div>
  <ul class="pagination pagination-lg pagination-colory">

<?php

  if($pages >= 1 && $page <= $pages){
    for($x=1; $x<=$pages; $x++){    
      echo ($x == $page) ? '<li class="active"><a href="home.php?p=poliknik&hal='.$x.'">'.$x.'</a></li>' : '<li><a href="home.php?p=poliknik&hal='.$x.'">'.$x.'</a></li>';
    }
  } 

?>

  </ul>
 </div>
</center>

您可以使用 mysql &gt;检查我的分页代码http://pastebin.com/CkyazRZM

2 个答案:

答案 0 :(得分:2)

编辑此行

$pages = ceil(mysqli_free_result($page_query, 0) / $per_page);

$pages = ceil(mysqli_fetch_array($page_query)[0] / $per_page);
mysqli_free_result($page_query);

答案 1 :(得分:0)

MySQLi扩展程序没有mysqli_result函数(它有mysqli_result ),mysqli_free_result函数完全执行其他操作。< / p>

您必须使用mysqli_fetch_row()或类似内容获取行并访问数据:

$query = "SELECT COUNT(*) FROM poliknik";
$page_query = mysqli_query($con,$query);
$r = mysqli_fetch_row ($page_query);
$count = $r[0];
$pages = ceil($count / $per_page);

不要忘记检查mysqli_query()mysqli_fetch_row()的返回值,并妥善处理错误。