分页数据库查询困境

时间:2013-05-02 19:10:37

标签: php html mysql

我一整天都在这工作,但仍然在晚上8点没有运气。我想知道你们是否可以就修复它给我一些建议。我正在为大学建立一个Upload-gif共享系统。

无论如何,这是我的代码 -

 <?php
 ini_set('display_errors',1); 
 error_reporting(E_ALL);

 mysql_connect("localhost","root","root") or die("Top Query");
 mysql_select_db("UPLOAD") or die(mysql_error());   

 $count_query = mysql_query("SELECT NULL FROM details");
 $count = mysql_num_rows($count_query); 

 //pagination 

 if(isset($_GET['page'])){
$page = preg_replace("#[^0-9]#","",$_GET['page']);  
 }else{
$page= 1;
 }
 $perPage = 5;
 $lastPage = ceil($count / $perPage);

if($page < 1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}
$limit = "LIMIT " .($page -1) * $perPage . ", $perPage";

//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC") or die("2nd Query"); 
//Puts it into an array 


$pagination="";
if($lastPage != 1){

if($page != $lastPage){
    $next = $page + 1;
    $pagination.='<a href="index.php?page='.$next.'">More</a>';
}

if($page != 1){
    $prev = $page - 1;
    $pagination.='<a href="index.php?page='.$prev.'">Back</a>';
}
}
?>

然后输出html -

<?php 
    while($info = mysql_fetch_array($query)){ 
        $shortlink = "<a href=uploads/".$info['photo'].">".$info['photo']."                   </a>" ;

    //Outputs the image and other data
    echo "<article class='upload-post'>" . "<div class='crop'>";
    echo "<a href=uploads/".$info['photo'].">";
    echo "<img class='scale-with-grid' src=uploads/".$info['photo'] .">"."</a>"; 
    echo "</div>";
    echo "".$info['name'] . "<br/>"; 
    echo "Reaction ".$info['reaction'] ."<br/>";
    echo "In " .$info['category'] ." <br/>";
    echo "On " .$info['date_added'] ." <br/>";
    echo "Link: $shortlink";
    echo "</article>";
 }


?>

 <?php echo $pagination;?>

我可以在页面之间切换但不限制页面上显示的帖子数量。我非常感谢帮助,因为截止日期并不太远。 提前一堆谢谢!

2 个答案:

答案 0 :(得分:1)

您永远不会将$limit附加到您的查询中。

答案 1 :(得分:0)

尝试以下作为替代......

...

//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC ".$limit) or die("2nd Query"); 
//Puts it into an array

...