Php分页和MYSQL区分

时间:2012-11-26 11:51:53

标签: php mysql

好的我有一个基本的php分页脚本,它有一个基本的下一个和上一个按钮。现在这个工作正常,直到我添加一个distinct子句。请参阅下面的代码。

$query = "SELECT COUNT(*) as num FROM $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
echo "$total_pages";
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data

$query1 = "SELECT Distinct model_group from $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;

现在,如果我将查询1中的查询更改为

 $query = "SELECT * From

代码工作正常,下面是我的下一个和上一个按钮的代码。

Previous Button

  if ($page > 1){
        echo "<a href='$targetpage?page=$prev&type=$type&manufacturer=$manufacturer&year=$year'><div class='previous'><img src='images/PrevButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
    }else{
        echo "<span class='disabled'><div class='previous'>Previous</span></span>"; }

Next Button


if ($page < $lastpage){ 
        echo "<a href='$targetpage?page=$next&type=$type&manufacturer=$manufacturer&year=$year'><div class='next'><img src='images/MoreButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
    }else{
        echo "<span class='disabled'><div class='next'>More</span></span>";
    }

无论如何,我可以在计数查询中包含不同的值吗?因为我认为它会为第二个查询返回不同的值。

1 个答案:

答案 0 :(得分:1)

我认为你必须使用

SELECT COUNT(DISTINCT *)

在你的第一个声明中

mysql中更好的方法是使用select中的SQL_CALC_FOUND_ROWS选项修复分页问题:

SELECT SQL_CALC_FOUND_ROWS * FROM mysql.user LIMIT 1

你的结果将是1行...但是

SELECT FOUND_ROWS();

你成为没有限制的结果的计数!