Home >Backend Development >PHP Tutorial >用PHP实现ODBC数据分页显示一例_php基础

用PHP实现ODBC数据分页显示一例_php基础

WBOY
WBOYOriginal
2016-05-17 09:44:37977browse

$pagesize = 2; //一页显示记录数

$con = odbc_connect("access_test","","",SQL_CUR_USE_ODBC) or die("无法连接ODBC数据源access_test"); //连接一个ODBC数据源
$sql = "select count(*) as total from test"; //取得记录总数SQL语句
$rst = odbc_exec($con,$sql) or die("$sql查询出错"); //执行取得记录总数SQL语句
$recordcount = odbc_result($rst,1); //取得记录总数,在这里也可以用$recordcount = odbc_result($rst,"total");
odbc_free_result($rst); //释放资源

$pagecount = bcdiv($recordcount+$pagesize-1,$pagesize,0); //算出总页数

if(!isset($page)) $page = 1; //如果没有指定显示页码,缺省为显示第一页
if($pageif($page>$pagecount) $page = $pagecount; //如果页码比总页数大,则显示最后一页

if($page>0){ //页码比0大,表示有数据
   echo '>> 分页 ';
   echo '首页 ';
   if($page>1){
      echo '前页 ';
   }
   else{
      echo '前页 ';
   }
   if($page      echo '后页 ';
   }
   else{
      echo '后页 ';
   }
   echo '尾页 ';
   echo '页次: ' . $page . '/' . $pagecount . '页 ';
   echo $pagesize . '条/页 ';
   echo '共' . $recordcount . '条 ';

   $sql = "select * from test"; //取得数据SQL语句
   $rst = odbc_exec($con,$sql) or die("$sql查询出错"); //执行取得数据SQL语句

   $fieldcount = odbc_num_fields($rst); //取得字段总数

   echo '

';
   echo '';
   for($i=1;$i      echo ''; //显示第$i个字段名
   }
   echo '';
   $rowi = ($page-1)*$pagesize+1;
   for($i=0;$i      echo '';
      if($rowi>$recordcount){
         for($j=0;$j            echo '';
         }
      }
      else{
         odbc_fetch_into($rst,$rowi,&$row);
         for($j=0;$j            $field = $row[$j];
            if($field=='') $field = ' ';
            echo '';
         }
         $rowi = $rowi+1;
      }
      echo '';
   }
   echo '
' . odbc_field_name($rst,$i) . '
 ' . $field  . '
';

   odbc_free_result($rst); //释放资源
}
else{
   echo "无数据";
}

odbc_close($con); //关闭连接并释放资源
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn