I've decided to post my code since it took me days to get this working properly.
Maybe someone can use this.
I'm not using ODBC to connect to the sqlsrv (which is a MS SQL server 2005 I think) but the php mssql extension.
Once I make a complete function out of this in which you can provide
- the query as a base table
- the rows returned as array
I'll post it here.
Furthermore : dataTables is nice but the documentation often lacks depth.
The paging query shown on the MSSQL ODBC page is simply wrong.
I believe the OVER sql-function should be used to get the paging done, but thus far I cannot get it to work like that.
Maybe someone can use this.
I'm not using ODBC to connect to the sqlsrv (which is a MS SQL server 2005 I think) but the php mssql extension.
Once I make a complete function out of this in which you can provide
- the query as a base table
- the rows returned as array
I'll post it here.
Furthermore : dataTables is nice but the documentation often lacks depth.
The paging query shown on the MSSQL ODBC page is simply wrong.
I believe the OVER sql-function should be used to get the paging done, but thus far I cannot get it to work like that.
$sQuery= "SELECT ".implode(",",$aColumns)." FROM (SELECT row_number() OVER (ORDER BY ".$sIndexColumn.") AS rownum, ".implode(",",$aColumns)." FROM $sTable $sWhere $sOrder ) AS A WHERE A.rownum BETWEEN ".$top." AND ".($top+$limit);hence I commented it and used hamidych's function.
<?php /* Indexed column (used for fast and accurate table cardinality) */ $sIndexColumn = "site_id"; /* DB table to use */ //$sTable = "site"; $sTable= " ( SELECT site_id ,(site_id + ' - ' + ISNULL(descr,'') + ISNULL(city,'') + ' (' + country_id + ')') as descr ,country_id from site WHERE country_id IN ('BEL','FRA') ) as tab "; /* Database connection information */ $gaSql['user'] = ""; $gaSql['password'] = ""; $gaSql['db'] = ""; $gaSql['server'] = ""; $connectionInfo = array("UID" => $gaSql['user'], "PWD" => $gaSql['password'], "Database"=>$gaSql['db'],"ReturnDatesAsStrings"=>true); $gaSql['link'] = sqlsrv_connect( $gaSql['server'], $connectionInfo); $params = array(); $options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET ); /* Columns */ $aColumns = array( 'site_id', 'descr', 'country_id'); /* $aColumns = array(); $cSql = "SELECT sys.columns.name colname FROM sys.tables JOIN sys.columns ON sys.columns.object_id = sys.tables.object_id WHERE sys.tables.name = '" . $sTable . "'"; $cResult = sqlsrv_query($gaSql['link'], $cSql ); while ( $r = sqlsrv_fetch_array( $cResult ) ) { array_push( $aColumns, $r['colname'] ); }; */ /* Ordering */ $sOrder = ""; if ( isset( $_GET['iSortCol_0'] ) ) { $sOrder = "ORDER BY "; for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ ) { if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ) { $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]." ".addslashes( $_GET['sSortDir_'.$i] ) .", "; } } $sOrder = substr_replace( $sOrder, "", -2 ); if ( $sOrder == "ORDER BY" ) { $sOrder = ""; } } /* Filtering */ $sWhere = ""; if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) { $sWhere = "WHERE ("; for ( $i=0 ; $i<count($aColumns) ; $i++ ) { $sWhere .= $aColumns[$i]." LIKE '%".addslashes( $_GET['sSearch'] )."%' OR "; } $sWhere = substr_replace( $sWhere, "", -3 ); $sWhere .= ')'; } /* Individual column filtering */ for ( $i=0 ; $i<count($aColumns) ; $i++ ) { if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' ) { if ( $sWhere == "" ) { $sWhere = "WHERE "; } else { $sWhere .= " AND "; } $sWhere .= $aColumns[$i]." LIKE '%".addslashes($_GET['sSearch_'.$i])."%' "; } } /* Paging */ $top = (isset($_GET['iDisplayStart']))?((int)$_GET['iDisplayStart']):0 ; $limit = (isset($_GET['iDisplayLength']))?((int)$_GET['iDisplayLength'] ):10; $sQuery = "SELECT TOP $limit ".implode(",",$aColumns)." FROM $sTable $sWhere ".(($sWhere=="")?" WHERE ":" AND ")." $sIndexColumn NOT IN ( SELECT $sIndexColumn FROM ( SELECT TOP $top ".implode(",",$aColumns)." FROM $sTable $sWhere $sOrder ) as [virtTable] ) $sOrder"; //$sQuery= "SELECT ".implode(",",$aColumns)." FROM (SELECT row_number() OVER (ORDER BY ".$sIndexColumn.") AS rownum, ".implode(",",$aColumns). // " FROM $sTable $sWhere $sOrder ) AS A WHERE A.rownum BETWEEN ".$top." AND ".($top+$limit); $rResult = sqlsrv_query($gaSql['link'],$sQuery) or die("$sQuery: " . sqlsrv_errors()); $sQueryCnt = "SELECT * FROM $sTable $sWhere"; $rResultCnt = sqlsrv_query( $gaSql['link'], $sQueryCnt ,$params, $options) or die (" $sQueryCnt: " . sqlsrv_errors()); $iFilteredTotal = sqlsrv_num_rows( $rResultCnt ); $sQuery = " SELECT * FROM $sTable "; $rResultTotal = sqlsrv_query( $gaSql['link'], $sQuery ,$params, $options) or die(sqlsrv_errors()); $iTotal = sqlsrv_num_rows( $rResultTotal ); $output = array( "sEcho" => intval($_GET['sEcho']), "iTotalRecords" => $iTotal, "iTotalDisplayRecords" => $iFilteredTotal, "aaData" => array() ); while ( $aRow = sqlsrv_fetch_array( $rResult ) ) { $row = array(); for ( $i=0 ; $i<count($aColumns) ; $i++ ) { if ( $aColumns[$i] != ' ' ) { $v = $aRow[ $aColumns[$i] ]; $v = mb_check_encoding($v, 'UTF-8') ? $v : utf8_encode($v); $row[]=$v; } } If (!empty($row)) { $output['aaData'][] = $row; } } echo json_encode( $output ); ?>