Liz
Liz

Reputation: 1048

Run MySQL query in DataTables server-side processing script

I am using DataTables server-side processing to pull data into a DataTables table from a MySQL table.

This is the working MySQL query I would like to run and display in my DataTables table:

$sql = "SELECT Client,EstimateNumber,Status,TotalEstimatedTime,CostToDateRoleTotal,ROUND((CostToDateRoleTotal/TotalEstimatedTime)*100) as PercentComplete FROM Estimates WHERE " . ($studioName != null ? "Studio = '" . $studioName. "' AND" : '') . " Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 AND CostToDateRoleTotal > 0 ORDER BY PercentComplete DESC";

I have adjusted the DataTables server-side processing script to be:

<?php

// connection configuration
require_once 'config.php';

// db table to use
$table = 'Estimates';

// table's primary key
$primaryKey = 'EstimateNumber';

$percent_complete = "('CostToDateRoleTotal'/'TotalEstimatedTime')*100";

// array of database columns which should be read and sent back to DataTables.
// the 'db' parameter represents the column name in the database, while the 'dt'
// parameter represents the DataTables column identifier.
$columns = array(
array('db' => 'Client', 'dt' => 0),
array('db' => 'EstimateNumber', 'dt' => 1),
array('db' => 'Status', 'dt' => 2),
array('db' => 'TotalEstimatedTime', 'dt' => 3),
array('db' => 'CostToDateRoleTotal', 'dt' => 4),
array('db' => $percent_complete, 'dt' => 4),
); // end columns array

// sql server connection information
$sql_details = array(
'user' => $currentConfig['user'],
'pass' => $currentConfig['pass'],
'db' => $currentConfig['name'],
'host' => $currentConfig['host'],
);

// DataTables helper class
require 'ssp.class.php';

function utf8ize($d) {
if (is_array($d)) {
    foreach ($d as $k => $v) {
        $d[$k] = utf8ize($v);
    }
} else if (is_string ($d)) {
    return utf8_encode($d);
}
return $d;
}


$data = SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, "Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 AND CostToDateRoleTotal > 0");

echo json_encode(utf8ize($data));

This line is throwing an error:

$percent_complete = "('CostToDateRoleTotal'/'TotalEstimatedTime')*100";

The error is: {"error":"An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column '('CostToDateRoleTotal'/'TotalEstimatedTime')*100' in 'field list'"}

In my original $sql query above, I ran this calculation and displayed the outcome as a new column, $percent_complete. I am trying to display those same results in my DataTables table. How can I alter my server-side processing script to perform this calculation and display it in a new column?

Upvotes: 1

Views: 1035

Answers (2)

Gyrocode.com
Gyrocode.com

Reputation: 58880

CAUSE

Class SSP defined in ssp.class.php cannot handle column aliases, expressions or JOINs.

SOLUTION

You need to alter ssp.class.php and remove all ` (backtick) characters that escape column and table names. You would be responsible for escaping column/table names yourself if the name is a reserved word.

Then replace

array('db' => $percent_complete, 'dt' => 4)

with

array('db' => 'ROUND((CostToDateRoleTotal/TotalEstimatedTime)*100)', 'dt' => 4)

Upvotes: 2

Mike
Mike

Reputation: 1

I don't know how the SSP class formats the query but you might want to try to add "AS percent_complete".

$percent_complete = "(CostToDateRoleTotal/TotalEstimatedTime)*100 AS percent_complete";

When you SELECT a column it needs a name.

Upvotes: -1

Related Questions