Using Mysqli instead of Mysql
Using Mysqli instead of Mysql
rubenvisscher
Posts: 2Questions: 0Answers: 0
Hello,
I want to use de server-side processing option from the Datatable. I've changed the php code to this :
[code]
<?php
/* MySQL connection */
include('config.php');
include('Database.php');
$db = new Database(DB_USER, DB_HOST, DB_PASSWORD, DB_NAME);
/* Paging */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
/* $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );*/
$sLimit = "LIMIT ".mysqli_real_escape_string ( $_GET['iDisplayStart'] ).", ".
mysqli_real_escape_string ( $_GET['iDisplayLength'] );
}
/* Ordering */
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
//for ( $i=0 ; $igetQuery($sQuery);
$aResultFilterTotal = $rResultFilterTotal->fetch_array();
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(id)
FROM ajax
";
//$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$rResultTotal = $db->getQuery($sQuery);
//$aResultTotal = mysql_fetch_array($rResultTotal);
$aResultTotal = $rResultTotal->fetch_array();
$iTotal = $aResultTotal[0];
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
//while ( $aRow = mysql_fetch_array( $rResult ) )
while ( $aRow = $rResult->fetch_array() )
{
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['engine']).'",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
function fnColumnToField( $i )
{
if ( $i == 0 )
return "engine";
else if ( $i == 1 )
return "browser";
else if ( $i == 2 )
return "platform";
else if ( $i == 3 )
return "version";
else if ( $i == 4 )
return "grade";
}
?>
[/code]
If I load the page, "server_processing.php" , in the browser I get the right JSON Code but he don't load the data in the table on the page , "server_side.html" .
Does someone know what goes wrong ?
I want to use de server-side processing option from the Datatable. I've changed the php code to this :
[code]
<?php
/* MySQL connection */
include('config.php');
include('Database.php');
$db = new Database(DB_USER, DB_HOST, DB_PASSWORD, DB_NAME);
/* Paging */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
/* $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );*/
$sLimit = "LIMIT ".mysqli_real_escape_string ( $_GET['iDisplayStart'] ).", ".
mysqli_real_escape_string ( $_GET['iDisplayLength'] );
}
/* Ordering */
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
//for ( $i=0 ; $igetQuery($sQuery);
$aResultFilterTotal = $rResultFilterTotal->fetch_array();
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(id)
FROM ajax
";
//$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$rResultTotal = $db->getQuery($sQuery);
//$aResultTotal = mysql_fetch_array($rResultTotal);
$aResultTotal = $rResultTotal->fetch_array();
$iTotal = $aResultTotal[0];
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
//while ( $aRow = mysql_fetch_array( $rResult ) )
while ( $aRow = $rResult->fetch_array() )
{
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['engine']).'",';
$sOutput .= '"'.addslashes($aRow['browser']).'",';
$sOutput .= '"'.addslashes($aRow['platform']).'",';
if ( $aRow['version'] == "0" )
$sOutput .= '"-",';
else
$sOutput .= '"'.addslashes($aRow['version']).'",';
$sOutput .= '"'.addslashes($aRow['grade']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
function fnColumnToField( $i )
{
if ( $i == 0 )
return "engine";
else if ( $i == 1 )
return "browser";
else if ( $i == 2 )
return "platform";
else if ( $i == 3 )
return "version";
else if ( $i == 4 )
return "grade";
}
?>
[/code]
If I load the page, "server_processing.php" , in the browser I get the right JSON Code but he don't load the data in the table on the page , "server_side.html" .
Does someone know what goes wrong ?
This discussion has been closed.
Replies
Allan
Valid JSON
[/code]
I have tried that but the JSON is valid
Allan
just returns a string:
[code]
$sOutput = '{"sEcho": 1, "iTotalRecords": 3, "iTotalDisplayRecords": 3, "aaData": [ ["Contact","email"],["Get A Quote","email"],["Online Certificate","email"]] }';
echo $sOutput;
[/code]
declaration and call - maybe it is a settings/options issue?
[code]
$j('#quotetable').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/wordpress/wp-content/themes/mytheme/get-quotes.php"
} );
[/code]
it fails inside the function() call
[code]this.fnServerData = function ( url, data, callback ) {
1254 $.ajax( {
1255 "url": url,
1256 "data": data,
1257 "success": callback,
1258 "dataType": "json",
1259 "cache": false,
1260 "error": function () {
1261 alert( "DataTables warning: JSON data from server failed to load or be parsed. "+
1262 "This is most likely to be caused by a JSON formatting error." );
1263 }
1264 } ); [/code]
leave the mysql_pconnect alone and just add a mysqli connection in a new addition to the array, and just use the mysqli connection. So the result is as follows:
[code]$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$gaSql['splink'] = new mysqli( $gaSql['server'], $gaSql['user'], $gaSql['password'], $gaSql['db'] ) or
die( 'Could not open connection to server' );
.
.
.
$res = $gaSql['splink']->multi_query($query);[/code]