Bug in the server side SQL example?

Bug in the server side SQL example?

bozdenbozden Posts: 10Questions: 0Answers: 0
edited November 2013 in DataTables 1.9
Hi,

I started to use Ajax with DT and started with the given PHP example. I have buttons on the rightmost column. So I define aColumns as:

[code]$aColumns = array( 'id', 'name', 'description', ' ');[/code]

But in this case an extra comma comes out in the SQL on this line:

[code]SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`[/code]

Like:
[code]SELECT SQL_CALC_FOUND_ROWS id, name, description, FROM ... [/code]

I added the following code to correct the issue:
[code]
/* SQL queries - Get data to display */
$sColumns = trim(str_replace(" , ", " ", implode(", ", $aColumns)));
if (substr($sColumns, -1) == ',') {
$sColumns = substr($sColumns, 0, strlen($sColumns)-1);
}
$q = "SELECT SQL_CALC_FOUND_ROWS ". $sColumns ." FROM $sTable $sWhere $sOrder $sLimit ";
$rResult = mysqli_query($db, $q) or die(mysqli_error($db));
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The $aColumns array wasn't designed to be able to take a space as a value. Indeed, I suspect that probably isn't the only issue with that script using a space in $aColumns .

    However, the script is intended as a starting point, so its good to hear you've got a work around for your extension to it!

    Allan
This discussion has been closed.