1

到目前为止,我一直在使用 jqgrid 和 mysql 和 php。我的代码适用于 jqGrid Demos 站点中给出的示例。
javascript部分提供的数据有:

  • 页=1
  • 行 =8
  • sord=asc

    $page = $_GET['page']; // get the requested page
    $limit = $_GET['rows']; // get how many rows we want to have into the grid 
    $sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
    $sord = $_GET['sord']; // get the direction 
    if(!$sidx) $sidx =1; // connect to the database    
    $connection = mysql_connect($serveur,$user,$password);
    $db = mysql_select_db($bdd, $connection); 
    mysql_query("set names 'utf8'");
    $query = "SELECT COUNT(*) AS count FROM Preferences WHERE (Id_Membre ='$idm')";
    $result = mysql_query($query,$connection); 
    $row = mysql_fetch_array($result); 
    $count = $row['count']; 
    if( $count > 0 && $limit > 0) { 
        $total_pages = ceil($count/$limit); 
    }
    else { 
        $total_pages = 0; 
    } 
    
    if ($page > $total_pages) $page=$total_pages; 
    
    $start = $limit*$page - $limit; // do not put $limit*($page - 1) 
    if ($start<0) $start = 0;
    $query = "select * from Preferences where (Id_Membre ='$idm') order by $sidx $sord LIMIT $start , $limit";
    $result = mysql_query($query, $connection);
    

最后一个查询返回 4 行。

这是适用于 Postgresql 的相同代码。使用相同的数据,此代码不返回任何内容!

    $page = $_GET['page']; // get the requested page
    $limit = $_GET['rows']; // get how many rows we want to have into the grid 
    $sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
    $sord = $_GET['sord']; // get the direction 
    if (!$sidx) $sidx =1; // connect to the database    
    $connection = pg_connect($con);

    pg_query($connection,"set names 'utf8'");
    $query = "SELECT COUNT(*) AS count FROM preference WHERE (id_membre ='$idm')";
    $result = pg_query($connection,$query); 
    $row = pg_fetch_array($result); 
    $count = $row['count']; 
    if( $count > 0 && $limit > 0) { 
        $total_pages = ceil($count/$limit); 
    }
    else { 
        $total_pages = 0; 
    } 

    if ($page > $total_pages) $page=$total_pages; 

    $start = $limit*$page - $limit; // do not put $limit*($page - 1) 
    if ($start<0) $start = 0;  
    $query = "select * from preference where (id_membre ='$idm') order by $sidx $sord  LIMIT $start OFFSET $limit";
    $result = pg_query($connection,$query);    

有任何想法吗 ?我以为limit 0,8变成了limit 0 offset 8

4

1 回答 1

1

mysql中的limit 0,8意味着postgres中的limit 8 offset 0

$query = "select * from preference where (id_membre ='$idm') 
    order by $sidx $sord LIMIT $limit OFFSET $start";
于 2013-05-04T01:16:36.813 回答