【问题标题】:Paging in php mysqlphp mysql中的分页
【发布时间】:2013-01-16 21:44:06
【问题描述】:

我正在尝试制作一个包含电影的网站,一切都很好,但我只有一个小问题,每当我制作网站时,我都会在本地计算机上完成所有工作测试然后我上传网络,下面的代码用于使用查询进行分页,它在 WAMP(本地)中运行良好。但是当我将分页代码上传到我的网络服务器时,它显示不存在。 它显示了else部分,有什么问题?

<?php

$per_page = 35; 
 $page = 1;
 if (isset($_GET['page'])) 
 {
  $page = intval($_GET['page']); 
  if($page < 1) $page = 1;
}

 $start_from = ($page - 1) * $per_page; 

$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
 mysql_select_db('saryaal_com_movies',$con);

 $current_items = mysql_query( "SELECT * FROM `english` LIMIT $start_from, $per_page");
 if( mysql_num_rows($current_items) > 0)
 {
  while($item = mysql_fetch_assoc($current_items))
  {

  ?>
    <tr>
        <td> <strong><a href="english/english-preview.php?id=<?php echo$item['id']?>" ><?php echo $item['title'] ;?></a>    </strong></td>
        <td> <strong> <?php echo $item['year'] ;?>  </strong></td>
        <td> <strong> <?php echo $item['quality'] ;?>   </strong> </td>
    </tr>
    <tr><td>
    <?php
    }
 }
 else
 {
  echo 'this page does not exists'; 
 }

 $total_rows = mysql_query("SELECT COUNT(*) FROM `english`");
 $total_rows = mysql_fetch_row($total_rows);
 $total_rows = $total_rows[0];

 $total_pages = $total_rows / $per_page;
 $total_pages = ceil($total_pages); # 19/5 = 3.8 ~=~ 4

 for($i = 1; $i  <= $total_pages; ++$i)
 {
  echo "<a href='temp2.php?page=$i' class='pagNumActive'>$i</a> &nbsp;&nbsp;";
 }
 ?>

【问题讨论】:

  • “不存在”是什么意思?由于您不再在 localhost 上运行,因此您可能需要更改数据库凭据以匹配 Web 服务器的凭据。
  • 自将您的网站上传到网络服务器后,您的数据库连接详细信息是否已更新?
  • error_reporting(E_ALL) 放在顶部,看看它给出了什么错误。我的赌注是无法连接到 MySQL
  • 确保您的虚拟主机具有相同的 db/tables/user/pass
  • 我的整个网络都在工作,这段代码也在工作,直到我将其更改为“...WHERE 类型如 '%$genre%' LIMIT $start_from, $per_page”

标签: php mysql database paging


【解决方案1】:
<?php if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
 $sql=mysql_query("select * from job where AND status='Active'");
 } 
$per_page = 5;
 $page = 1; 
if (isset($_GET['page'])) { 
$page = intval($_GET['page']);
 if($page < 1) $page = 1; 
} 
$start_from = ($page - 1) * $per_page;
 if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") 
{ 
$current_items=mysql_query("select * from job LIMIT $start_from, $per_page"); } $start_from, $per_page");
 if( mysql_num_rows($current_items)>0) { while($arr=mysql_fetch_array($current_items)) {
?> 

<?php include("include/result.php") ?>// result u want to display 
<?php 
} 
} else { 
echo 'Data does not exists'; } 
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
 $total_rows=mysql_query("select COUNT(*) from job where AND status='Active'");
 } 
$total_rows = mysql_query("SELECT COUNT(*) FROM job");
 $total_rows = mysql_fetch_row($total_rows); 
$total_rows = $total_rows[0]; 
$total_pages = $total_rows / $per_page;
 $total_pages = ceil($total_pages);
 # 19/5 = 3.8 ~=~ 4 
echo "<div style='margin-left:280px;'>";
 echo "Page : ";
 for($i = 1; $i <= $total_pages; $i++) {
 echo "[<a style='text-decoration:none' href='search_result.php?page=$i' class='pagNumActive'>$i</a>&nbsp;]"; 
} 
echo "</div>"; 
?> 

【讨论】:

    【解决方案2】:

    这是你的问题:

    $con= mysql_connect("localhost","sarya_asad","Thisisfor123");
        mysql_select_db('saryaal_com_movies',$con);
    

    您需要更改主机详细信息。 localhost 是您机器上的本地服务器。

    【讨论】:

    • 这可能是正确的答案,但根据服务器的不同,它可能允许 localhost 作为主机。
    【解决方案3】:

    改变这个

     $start_from = ($page - 1) * $per_page;
    

     $start_from = ($page) * $per_page;
    

    【讨论】:

    • 第一页你会得到 LIMIT 35,35 而不是 LIMIT 0,35
    • 不使用LIMIT $start_from, $per_page 试试看是否有效
    • 我的查询工作正常,直到我更改我的查询 SELECT id,title,year,quality,genre FROM chinese WHERE Genre like '%$genre%' LIMIT $start_from, $per_page
    • 你的代码看起来不错,试试把你的查询改成这个SELECT * FROM english` LIMIT '$start_from', '$per_page'`
    【解决方案4】:

    你可能想使用类似的东西:

    $per_page = 35;
    $start_from = $page * $per_page - $per_page;
    

    【讨论】:

      【解决方案5】:

      试试这个逻辑分页:

      您的目标是抵消您希望在每页上显示的图像数量。

      假设您想在一个页面上显示 6 个电影缩略图...您将拥有:

      $videos_per_page = 6
      $pageNumber = (isset($_GET['page']) ? ($_GET['page']) : 1);
      

      你的偏移量是:

      $offset = $videos_per_page * $pageNumber 
      

      (6 个视频 * 第 0 页...所以您偏移了 0 个视频。这很好,因为您希望在第 0 页的数据结构中显示前 6 个视频)。


      现在您有了偏移值...您需要将数组指针设置到正确的位置...循环遍历存储电影的数据库行并将指针移动您的偏移值...将其存储在$videos_to_offset...

      while ($videos_per_page < $offset && ($row = $Query_Result->fetch_assoc())) {
          $videos_to_offset++;
      }
      

      现在您可以遍历数据库行,从偏移数组指针停止的位置输出视频:

          $video_counter = 0;
          while ($video_counter < $videos_per_page && ($row = $Query_Result->fetch_assoc())) {
      
              echo   $row['videopath'];
              $video_counter++;
      }
      

      类似的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-06
        • 2013-05-25
        • 2018-01-12
        • 2011-04-14
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        相关资源
        最近更新 更多