mysql - jqgrid 分页与 postgresql

标签 mysql postgresql jqgrid

到目前为止,我一直在使用 jqgrid 和 mysql 以及 php。我的代码适用于 jqGrid 演示站点中给出的示例。
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

最佳答案

limit 0,8 在 mysql 中意味着 limit 8 offset 0 在 postgres 中。

$query = "select * from preference where (id_membre ='$idm') 
    order by $sidx $sord LIMIT $limit OFFSET $start";

关于mysql - jqgrid 分页与 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16368336/

相关文章:

php - 如果数据表中的计数值为空,则隐藏空 td

postgresql - 在使用 Kubernetes/minikube 进行本地开发时,我应该如何连接到本地主机上运行的 postgres 数据库?

javascript - 如何使用 jqGrid editoptions 值的函数创建有效的字符串?

javascript - 面临 JQ 网格问题

mysql当年与当年+6个月

mysql - 如何使用 Node.js Lambda 函数与 RDS MySQL 实例交互?

postgresql - hstore 键的唯一索引或约束

postgresql - 从企业架构师连接到 postgresql

具有自动完成功能的 JqGrid 无法解析从 Controller 到 View 的数据

mysql - 当主表与另一个多对一关系表连接时,字段的 SUM 相乘