Home  >  Article  >  Backend Development  >  Example of ODBC data paging using PHP

Example of ODBC data paging using PHP

WBOY
WBOYOriginal
2016-08-08 09:34:101129browse

$pagesize = 2; //Display the number of records on one page

$con = odbc_connect("access_test","","",SQL_CUR_USE_ODBC) or die("Unable to connect to ODBC data source access_test"); //Connect to an ODBC data source
$sql = "select count(*) as total from test"; //Get the total number of records SQL statement
$rst = odbc_exec($con,$sql) or die("$sql query error"); //Execute the SQL statement to obtain the total number of records
$recordcount = odbc_result($rst,1); //Get the total number of records, you can also use $recordcount = odbc_result($rst,"total");
odbc_free_result($rst); //Release resources

$pagecount = bcdiv($recordcount+$pagesize-1,$pagesize,0); //Calculate the total number of pages

if(!isset($page)) $page = 1; //If no page number is specified, the default is to display the first page
if($page<1) $page = 1; //If the page number is smaller than 1, display the first page
if($page>$pagecount) $page = $pagecount; //If the page number is greater than the total number of pages, display the last page

if($page>0){ //The page number is larger than 0, indicating that there is data
echo '>> paging ';
echo 'Homepage ';
If($page>1){
echo 'Previous page ';
}
else{
echo 'previous page';
}
If($page<$pagecount){
echo 'Next page ';
}
else{
echo 'next page';
}
echo 'Last page ';
echo 'Page: ' . $page . '/' . $pagecount . 'Page ';
echo $pagesize . 'Article/page';
echo 'total' . $recordcount . 'articles';
​​
$sql = "select * from test"; //Get data SQL statement
$rst = odbc_exec($con,$sql) or die("$sql query error"); //Execute the SQL statement to obtain data
​​
$fieldcount = odbc_num_fields($rst); //Get the total number of fields
​​
echo '

';
echo '';
for($i=1;$i<=$fieldcount;$i++){
echo ''; //Display the $i-th field name
}
echo '';
$rowi = ($page-1)*$pagesize+1;
for($i=0;$i<$pagesize;$i++){
echo '';
If($rowi>$recordcount){
for($j=0;$j<$fieldcount;$j++){
echo '';
         }
      }
       else{
          odbc_fetch_into($rst,$rowi,&$row);
for($j=0;$j<$fieldcount;$j++){
               $field = $row[$j];
                     if($field=='') $field = ' ';
echo '';
          }
           $rowi = $rowi+1;
      }
echo '';
}
echo '
' . odbc_field_name($rst,$i) . '
 ' . $field . '
';
​​
odbc_free_result($rst); //Release resources
}
else{
echo "No data";
}

odbc_close($con); //Close the connection and release resources
?>


The above introduces the example of using PHP to implement ODBC data paging, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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