【问题标题】:PHP Pagination issue mysql and php showing same data on all pagesPHP分页问题mysql和php在所有页面上显示相同的数据
【发布时间】:2013-03-01 14:39:39
【问题描述】:

我在使用 PHP 中从 MySQL 数据库获取的数据时遇到了一些分页问题。

我的代码如下。基本上发生的情况是它创建了适量的页面,但是每个页面显示相同的数据,甚至每页仅显示 5 行。

我真的被困住了。这是我第一次尝试分页。任何帮助将不胜感激。

        $pagenum = $_GET['pagenum'];

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

        //Count the number of results.
        $data = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id'") or die(mysql_error()); 
        $rows = mysql_num_rows($data);

        //Set the number of results to be displayed per page.
        $page_rows = 5;

        //This tells us the page number of our last page 
        $last = ceil($rows/$page_rows); 

         //this makes sure the page number isn't below one, or more than our maximum pages 
        if ($pagenum < 1) 
        { 
            $pagenum = 1; 
        }   elseif ($pagenum > $last) 
            { 
                $pagenum = $last; 
            }

        //This sets the range to display in our query 
        $max = 'LIMIT ' . ($pagenum - 1) * $page_rows .',' .$page_rows;

        //This is the query again, the same one... the only difference is we add $max into it.
        $data_p = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id' '$max'") or die(mysql_error());

        //Work out writers earnings based on prices.
        //100 Words - $1.25
        //300 Words - $2.50
        //500 Words - $4.00
        //700 Words - $5.50
        //1000 Words - $8.00
        $_100earnings = "0.65";
        $_300earnings = "1.25";
        $_500earnings = "2.50";
        $_700earnings = "3.00";
        $_1000earnings = "5.00";
    ?>
    <!-- main -->
    <div id="main">
    <center><h2>Write Articles</h2></center>
    <br />Available Projects:
    <table border="1">
        <tr>
            <td>Title:</td>
            <td>Length:</td>
            <td>Writers Earnings:</td>
        </tr>
        <?php
            //This is where you display your query results
            while($info = mysql_fetch_array($data_p))
            { 
                echo "<tr>";
                echo "<td>" . $info['keywords'] . "</td>";
                echo "<td>" . $info['length'] . "</td>";
                switch ($info['length'])
                {
                    case 100:
                        $writersearnings = $_100earnings;
                        break;
                    case 300:
                        $writersearnings = $_300earnings;
                        break;
                    case 500:
                        $writersearnings = $_500earnings;
                        break;
                    case 700:
                        $writersearnings = $_700earnings;
                        break;
                    case 1000:
                        $writersearnings = $_1000earnings;
                        break;
                }
                echo "<td>$" . $writersearnings . "</td>";
                //echo $info['Name']; 
                echo "</tr>";
            }

        ?>
    </table>
    <br /><br />
    <?php
        // This shows the user what page they are on, and the total number of pages
        echo " --Page $pagenum of $last-- <p>";


        // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.  
        if ($pagenum == 1) 
        {
        }   else 
            {
                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
                 echo " ";
                 $previous = $pagenum - 1;
                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";   
            } 

        //just a spacer
        echo " ---- ";

        //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
        if ($pagenum == $last) 
        {

        }   else 
            {
                 $next = $pagenum + 1;

                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";

                 echo " ";

                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
            }

【问题讨论】:

标签: php mysql pagination


【解决方案1】:

使用此代码,在查询中删除$max 周围的',当您在$max 周围添加' 时,查询变为select.... from.... where..... 'LIMIT.....',并且查询失败。

$data_p = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id' $max") or die(mysql_error());

【讨论】:

  • 那行得通。谢谢你。 ' 产生问题的任何原因?
  • @KriiV 接受答案是一个好习惯,当它解决您的问题时。 :)
【解决方案2】:

您需要在查询LIMIT offset,count 中指定LIMIT

$data_p = mysql_query("SELECT * FROMarticlesWHEREcontent= '' ANDrequestedby!= '$id' '$max'") or die(mysql_error());

'$max' 中删除' 引号

$data_p = mysql_query("SELECT * FROM articles WHERE content= '' AND requestedby!= '".$id."' ".$max) or die(mysql_error());

【讨论】:

  • 有效!谢谢你。为什么会造成问题?
  • 请不要使用 mysql_* 函数来编写新代码。它们不再维护,社区已开始弃用过程。看到红框了吗?相反,您应该了解准备好的语句并使用 PDO 或 MySQLi。
  • 因为mysql会把'里面的任何东西都当成一个字符串但实际上它是一个关键字,所以它周围不应该有任何引号
  • @PrasanthBendra 最好实际使用包含链接的评论,然后其中的语句更有意义..
猜你喜欢
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 2015-10-20
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多