Using PDO queries in place of mysql i.e deprecated - php

As we all know Mysql has been removed in php v7.0 i am trying to use pdo for fetching data (server side) using datatables using the following example but its in mysql need it in pdo:
CODE:
COLUMNS:
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array( 'first_name', 'last_name', 'position', 'office', 'salary' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "datatables_demo";
Creating PDO CONNECTION:
$db_host = "localhost";
$db_name = "sadad";
$db_user = "root";
$db_pass = "root";
try{
$db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
THE FOLLOWING CODE:
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".$db_con->quote( $_GET['iDisplayStart'] ).", ".
$db_con->quote( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
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] ) ]."
".$db_con->quote( $_GET['sSortDir_'.$i] ) .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= $aColumns[$i]." LIKE '%".$db_con->quote( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".$db_con->quote($_GET['sSearch_'.$i])."%' ";
}
}
$my = str_replace(" , ", " ", implode(", ", $aColumns));
/*
* SQL queries
* Get data to display
*/
$sQuery = $db_con->query("SELECT {$my} FROM {$sTable} {$sWhere} {$sOrder} {$sLimit}")->fetchAll();
//$rResult = ( $sQuery );
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
//$rResultFilterTotal = $sQuery;
$aResultFilterTotal = $sQuery;
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = $db_con->query( $sQuery ) or die(mysql_error());
$aResultTotal = $rResultTotal->fetchAll();
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = $rResult->fetchAll() )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
ERROR:
but i am getting the error ,i am unaware what to change in the above ,kind of getting started in pdo,an updated answer with code would be appreciated:
UPDATED THE CODE NOW RECEIVING FOLLOWING ERROR
[28-Aug-2018 16:58:39 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''asc' LIMIT '0', '50'' at line 2' in C:\MAMP\htdocs\backend\my.php:131
Stack trace:
#0 C:\MAMP\htdocs\backend\my.php(131): PDO->query('SELECT first_na...')
#1 {main}
thrown in C:\MAMP\htdocs\backend\my.php on line 131
**ERROR:**

I see a problem here:
$sWhere .= $aColumns[$i]." LIKE '%".$db_con->quote($_GET['sSearch_'.$i])."%' ";
The PDO::quote() function has a different output than the old deprecated mysql_real_escape_string() function.
Suppose your string is "O'Reilly" and you need the apostrophe character escaped.
mysql_real_escape_string("O'Reilly") will return:
O\'Reilly
Whereas $db_con->quote("O'Reilly") will return:
'O\'Reilly'
The quote() function adds string delimiters to the beginning and end of the string. This makes it work the same as the MySQL builtin function QUOTE()
So when you use PDO::quote() the way you're doing:
$sWhere .= $aColumns[$i]." LIKE '%".$db_con->quote($_GET['sSearch_'.$i])."%' ";
The resulting SQL clause looks like:
... WHERE mycolumn LIKE '%'search'%' ...
This is not going to work. You need it to be:
... WHERE mycolumn LIKE '%search%' ...
One solution is to add the % wildcards and then quote the result:
$sWhere .= $aColumns[$i]." LIKE ".$db_con->quote('%'.$_GET['sSearch_'.$i].'%') ;
Now it takes advantage of PDO::quote() adding the string delimiters.
By the way, I find all the . string concatenation makes PHP look awful. It's hard to write the code, and hard to read and debug it. I prefer using variables directly inside the string. And don't be afraid of doing the work in two lines of code. It's not always the best thing for code readability to stuff too much into one line.
$pattern = $db_con->quote("%{$_GET["sSearch_$i"]}%");
$sWhere .= "{$aColumns[$i]} LIKE {$pattern}";
But there's another way that is easier AND more secure.
Use query parameters instead of escaping/quoting.
$params[] = "%{$_GET["sSearch_$i"]}%";
$sWhere .= "{$aColumns[$i]} LIKE ?";
Then later...
$stmt = $db_con->prepare($sQuery);
$stmt->execute($params);
while ($row = $stmt->fetchAll()) {
...
}
Using parameters is simpler than using escaping/quoting. You don't have to mess around wondering if your quotes are balanced properly, because the parameter placeholder ? doesn't need string delimiters around it.
If you're learning PDO, I suggest doing some reading:
https://phpdelusions.net/pdo - a good tutorial.
http://php.net/pdo - reference documentation.
Both are important and useful. Reference docs are not as good for learning, but they're useful after you do the tutorial, to remind yourself of syntax, arguments, return values, etc. I've done plenty of PDO coding, but I open reference docs frequently.

Related

Passing Array from AJAX to PHP, then passing data back as JSON

I am having a strange issue with my PHP / JSON data being returned by PHP. Here is my PHP:
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
// add your columns here!!!
$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
//$aColumns = $_POST['selcolumns'];
//$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
$server = "";
$database = array("Database" => "");
$conn = sqlsrv_connect($server, $database);
if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true));
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "GUID";
/* DB table to use */
$sTable = $_POST['table'];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ( $sErrorMessage = '' ) {
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
/* Ordering */
$sOrder = "";
if ( isset( $_POST['order'] ) ) {
$sOrder = "ORDER BY ";
if ( $_POST['columns'][0]['orderable'] == "true" ) {
$sOrder .= "".$aColumns[ intval( $_POST['order'][0]['column'] ) ]." ".
($_POST['order'][0]['dir']==='asc' ? 'asc' : 'desc');
}
}
/* escape function */
function mssql_escape($data) {
if(is_numeric($data))
return $data;
$unpacked = unpack('H*hex', $data);
return '0x' . $unpacked['hex'];
}
/* Filtering */
$sWhere = "";
if ( isset($_POST['search']['value']) && $_POST['search']['value'] != "" ) {
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$sWhere .= $aColumns[$i]." LIKE '%".addslashes( $_POST['search']['value'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( isset($_POST['columns'][$i]) && $_POST['columns'][$i]['searchable'] == "true" && $_POST['columns'][$i]['search']['value'] != '' ) {
if ( $sWhere == "" ) {
$sWhere = "WHERE ";
}
else {
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".addslashes($_POST['columns'][$i]['search']['value'])."%' ";
}
}
/* Add the custom Date/Time filter */
if ( $sWhere == "" ) {
$sWhere = "WHERE (TimeOccurred >= "."'".$_POST['datestart']."'"." AND TimeOccurred <= "."'".$_POST['dateend']."')";
}
else {
$sWhere .= " AND (TimeOccurred >= "."'".$_POST['datestart']."'"." AND TimeOccurred <= "."'".$_POST['dateend']."')";
}
/* Paging */
$top = (isset($_POST['start']))?((int)$_POST['start']):0 ;
$limit = (isset($_POST['length']))?((int)$_POST['length'] ):5;
$sQuery = "SELECT TOP $limit ".implode(', ', $aColumns)." FROM $sTable $sWhere ".(($sWhere=="")?" WHERE ":" AND ")." $sIndexColumn NOT IN ( SELECT TOP $top $sIndexColumn FROM $sTable $sOrder ) $sOrder";
$rResult = sqlsrv_query($conn, $sQuery);
if($rResult === false){
die(sqlsrv_errors(SQLSRV_ERR_ERRORS));
}
/* Data set length after filtering */
$sQueryCnt = "SELECT * FROM $sTable $sWhere";
$rResultCnt = sqlsrv_query($conn, $sQueryCnt, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
$iFilteredTotal = sqlsrv_num_rows( $rResultCnt );
/* Total data set length */
$sQuery = "SELECT COUNT(GUID) FROM $sTable";
$rResultTotal = sqlsrv_query($conn, $sQuery, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
$aResultTotal = sqlsrv_fetch_array($rResultTotal, SQLSRV_FETCH_NUMERIC);
$iTotal = $aResultTotal[0];
/* Output */
$output = array(
"draw" => intval($_POST['draw']),
"recordsTotal" => $iTotal,
"recordsFiltered" => $iFilteredTotal,
"data" => array()
);
while ( $aRow = sqlsrv_fetch_array( $rResult, SQLSRV_FETCH_ASSOC) ) {
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$row[$aColumns[$i]] = $aRow[ $aColumns[$i] ];
}
$output['data'][] = $row;
}
echo json_encode( $output );
?>
The part of the code that has me stuck:
$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
//$aColumns = $_POST['selcolumns'];
//$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
If I leave my code as is and run through the process I get the data back as expected and everything works. I also get this output in my php-debug.txt:
Action TimeOccurred UserName IPv4From ShareName FullFilePath NewPathName FromServer
If I modify these lines of my code to this:
//$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
$aColumns = $_POST['selcolumns'];
$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
I do not get the data back as expected. I get a warning stating invalid JSON response and in my php-debug.txt I get this content:
Action TimeOccurred UserName IPv4From ShareName FullFilePath NewPathName FromServer
It is driving me bonkers that the array has the same values either way and yet it doesn't work. There is an extra space on the end of the php-debug.txt the 2nd time around, not sure where that comes from or if it is the problem.
Hopefully someone can point me in the right direction.
I figured out the problem. For some reason there is an extra element being added to my array, but its an empty element so when I print there is nothing there. I added this to my code array_pop($aColumns); and for now that resolved the problem. Its kind of a dirty workout though, and I can't help but think that eventually there will come a scenario where the last element isn't blank and its something I need. Would love to figure out a cleaner solution.

Adding custom field jQuery Datatables

I am using jQuery Datatables to populate my data. I am using the server side method which I need coz I am fetching hundred thousand records. However I don't know how to add a custom field. For example
|Action|
________
http://localhost/qms/public/customer/1/edit
Which the 1 should be the ID
Because on the datatables, you only declaring column tables that you need so I don't know how to add a custom one.
I currently have this:
I need to put action column to edit those customers. I am using Laravel 5.1
HTML View:
<table id="CustomerList" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th colspan="7"> <center>Customer Information<center></th>
<!-- <th colspan="2"> <center>Actions<center></th> -->
</tr>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Country</th>
<th>Postcode</th>
<!-- <th>Edit</th>
<th>Delete</th> -->
</tr>
</thead>
<tbody>
</tbody>
</table>
Ajax:
<script type="text/javascript">
$(document).ready(function() {
$.fn.dataTable.ext.legacy.ajax = true;
$('#CustomerList').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "api/customer/all",
"paging" : true,
"searching" : true,
"ordering" : true,
} );
var tt = new $.fn.dataTable.TableTools( $('#CustomerList').DataTable() );
$( tt.fnContainer() ).insertBefore('div.dataTables_wrapper');
});
</script>
Controller:
public function apiGetCustomers()
{
/*=================================================================*/
/*
* Script: DataTables server-side script for PHP and PostgreSQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array('id', 'firstname', 'lastname', 'gender', 'phone_num', 'country', 'postcode' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "phone_num";
/* DB table to use */
$sTable = "customers";
/* Database connection information */
$gaSql['user'] = "postgres";
$gaSql['password'] = "postgres";
$gaSql['db'] = "qms";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* DB connection
*/
$gaSql['link'] = pg_connect(
" host=".$gaSql['server'].
" dbname=".$gaSql['db'].
" user=".$gaSql['user'].
" password=".$gaSql['password']
) or die('Could not connect: ' . pg_last_error());
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayLength'] )." OFFSET ".
intval( $_GET['iDisplayStart'] );
}
/*
* Ordering
*/
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] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE This assumes that the field that is being searched on is a string typed field (ie. one
* on which ILIKE can be used). Boolean fields etc will need a modification here.
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" )
{
if($aColumns[$i] != 'id') // Exclude ID for filtering
{
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ")";
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
";
$rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iTotal = pg_num_rows($rResultTotal);
pg_free_result( $rResultTotal );
if ( $sWhere != "" )
{
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
$sWhere
";
$rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iFilteredTotal = pg_num_rows($rResultFilterTotal);
pg_free_result( $rResultFilterTotal );
}
else
{
$iFilteredTotal = $iTotal;
}
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
// Free resultset
pg_free_result( $rResult );
// Closing connection
pg_close( $gaSql['link'] );
}
Add headers for the custom fields, as you have above <th>Edit</th><th>Delete</th>
Use column rendering to add the content of the custom fields. Since you are using an "array of arrays" as datasource, you must do it this way :
small demonstration -> http://jsfiddle.net/pqgynvys/
var table = $('#example').DataTable({
data : data.data,
columns : [
null,
null,
null,
null,
null,
null,
null,
{ render : function() {
return '<button>edit</button>'
}
},
{ render : function() {
return '<button>delete</button>'
}
}
]
})
SOLUTION
In HTML add one column in the table header for Action.
In DataTables initialization options add columnDefs to target that 8th column ("targets": 7, zero-based index) and use render option to produce content for that column.
In the render function, you can use row variable to access the data for the row. Since you're returning array of arrays in your PHP script, you can access your ID by using row[0] (1st column, zero-based index).
var table = $('#CustomerList').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "api/customer/all"
"columnDefs": [
{
"targets": 7,
"render": function(data, type, row, meta){
return 'Edit';
}
}
]
});
DEMO
See this jsFiddle for code and demonstration.

How to populate a jquery datatable using server side processing (PHP)?

I'm trying to populate a datatable with a server side PHP script which echoes the data from a postgres table (~75K rows). I followed the steps given in the datatable page and implemented it, but the table doesn't show any data. This is what I have so long:
table definition in a jsp file:
<table id="myTable" class="table table-striped" width="100%">
<thead>
<tr>
<th>idpersona</th>
<th>primerapellido</th>
<th>primernombre</th>
<th>numeroidentificacion</th>
<th>fechanacimiento</th>
</tr>
</thead>
<tfoot>
<tr>
<th>idpersona</th>
<th>primerapellido</th>
<th>primernombre</th>
<th>numeroidentificacion</th>
<th>fechanacimiento</th>
</tr>
</tfoot>
</table>
Here is my function to initialise the table. I tried for hours (I'm a newbie programmer) to find the right folder where I must place the PHP file. Right now it is in the htdocs folder of my apache server (so I can access it from /localhost/tablabd.php). Is this the right way to do it?
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#myTable').dataTable( {
"Processing": true,
"ServerSide": true,
"sAjaxSource": "http://localhost/tablabd.php"
} );
} );
</script>
And finally the PHP script. When I type localhost/tablabd.php in my browser, all the data is fetched correctly. But when I execute my Java project, it doesn't show anything in the table 'myTable'.
<?php
/*
* Script: DataTables server-side script for PHP and PostgreSQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array("idpersona", "primerapellido","primernombre", "numeroidentificacion", "fechanacimiento");
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = '"idpersona"';
/* DB table to use */
$sTable = '"tpersonas"';
/* Database connection information */
$gaSql['user'] = "postgres";
$gaSql['password'] = "******";
$gaSql['db'] = "sisben";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* DB connection
*/
$gaSql['link'] = pg_connect(
" host=".$gaSql['server'].
" dbname=".$gaSql['db'].
" user=".$gaSql['user'].
" password=".$gaSql['password']
) or die('Could not connect: ' . pg_last_error());
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] )." OFFSET ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
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] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE This assumes that the field that is being searched on is a string typed field (ie. one
* on which ILIKE can be used). Boolean fields etc will need a modification here.
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ")";
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
";
$rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iTotal = pg_num_rows($rResultTotal);
pg_free_result( $rResultTotal );
if ( $sWhere != "" )
{
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
$sWhere
";
$rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iFilteredTotal = pg_num_rows($rResultFilterTotal);
pg_free_result( $rResultFilterTotal );
}
else
{
$iFilteredTotal = $iTotal;
}
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == 'idpersona' )
{
/* Special output formatting for 'ID' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
// Free resultset
pg_free_result( $rResult );
// Closing connection
pg_close( $gaSql['link'] );
?>
and a sample of the output of the script in the broswer: maybe I'm missing a column mapping somewhere?
{"sEcho":0,"iTotalRecords":74047,"iTotalDisplayRecords":74047,"aaData":[["e71657b3-a7f5-4a10-bc43-d0edbeb5cdab","PEREZ","ABDON","4299249","1947-07-10 00:00:00"],["796db2d4-fee3-4cca-ae06-429a2ea6c5af","TORREZ","MARIA","24240762","1951-09-17 00:00:00"]]}
Here is the info Firebug shows when I access the page on my application which contains the table:
_ 1440905636814
columns[0][data] 0
columns[0][name]
columns[0][orderable] true
columns[0][search][regex] false
columns[0][search][value]
columns[0][searchable] true
columns[1][data] 1
columns[1][name]
columns[1][orderable] true
columns[1][search][regex] false
columns[1][search][value]
columns[1][searchable] true
columns[2][data] 2
columns[2][name]
columns[2][orderable] true
columns[2][search][regex] false
columns[2][search][value]
columns[2][searchable] true
columns[3][data] 3
columns[3][name]
columns[3][orderable] true
columns[3][search][regex] false
columns[3][search][value]
columns[3][searchable] true
columns[4][data] 4
columns[4][name]
columns[4][orderable] true
columns[4][search][regex] false
columns[4][search][value]
columns[4][searchable] true
draw 1
length 20
order[0][column] 0
order[0][dir] asc
search[regex] false
search[value]
start 0
Thanks in advance.
SOLUTION
Correct option names are bProcessing and bServerSide. Your DataTables initialization code should be:
$('#myTable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/tablabd.php"
});
NOTES
I have changed URL to /tablabd.php because if your HTML and PHP are on different domain, Ajax calls may fail unless you allow cross-domain requests. Make sure you have HTML and PHP on the same domain.

SQL and DataTables Returning Values for the Database

Now before I ask I KNOW this is WAY OVER MY HEAD! I have no idea how to even begin editing this code. I have been DUMPED in it by some people I was working with and have been left to finish a project by myself! I have the code below and I would like to add in there
AND WHERE fieldname IS NOT NULL
Now for the original code where it needs to be added!
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_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 '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
I have NO IDEA where to even start. I hope someone can help, thanks in advance for baring with me!
///////// UPDATE / UPDATE / UPDATE / UPDATE / UPDATE/////////
Ok so I have found that the $sWhere only activates when a search field is populated! I need it to run on the data when it is initially pulled out and then still be active even when the search field is populated. What I have below is the SELECT query where a new $sQuery can be defined. I just need help to construct it to make sure it only pulls data WHERE fieldname IS NOT NULL. I have tried the obvious things with no luck. I am probably missing something fairly obvious. Thanks again for any help!!
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(`".$sIndexColumn."`)
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>
So this should be the final answer (me hopes:-))
Nah, another update:
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ') AND active IS NOT NULL';
}
/* 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 '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
if ( $sWhere != "" )
{
$sWhere .= " AND active IS NOT NULL ";
}else{
$sWhere = " WHERE active IS NOT NULL ";
}
Note that two things have changed:
The first query generator is for fulltext search over all fields, so it encapsulates the query in brackets and separates fields with an OR.
I added line 11: $sWhere .= ') AND active IS NOT NULL';
The second query generator is for individual column filtering which adds an LIKE clause to each of the specified columns. This where clause can be empty, so we need to check this before concatenating more parameters.
Updated:
I added line 31-36:
if ( $sWhere != "" )
{
$sWhere .= " AND active IS NOT NULL ";
}else{
$sWhere = " WHERE active IS NOT NULL ";
}
Now, with this update no rows with active is NULL will be displayed anymore. No matter if if you do a fulltext search, individual column filtering or just init the table. Maybe you should check if the total count is displayed correct. Are we there yet???
What you need to do know is to replace the active db-field with the one you want to ignore (Carers_Mod_Date) if it's NULL. You can add more excludes in these two lines separated with an AND.
Last tips: Always use some spaces around your concatenations because mysql will not mind to much spaces, but missing ones may produce an error.
Try:
$sWhere .= " (`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' AND `".$aColumns[$i]."`IS NOT NULL) ";
Also quickly made up from my Head. I'll recheck as soon as i find some old project i can modify.
Update: checked it. Works with indvidual column filtering!
Next Update:
Add something like this to your code, just before you encode your result:
$output['WHERE'] = $sWhere;
echo json_encode( $output );
Now , when you type in a searchterm, you can use the firebug (or else) console to see the response from your server and have the generated WHERE part of your query:
Hope this helps you to get along.
Try appending
if ($sWhere == "")
{
$sWhere = "WHERE";
} else {
$sWhere .= " AND"
}
$sWhere .= " fieldname IS NOT NULL";
after the last line of your existing code.

how to add names/tags to JSON data before encoding

I'm quite new to working with JSON.
My current script outputs the following JSON:-
{"comments":[[17.9775280899,"2011-09-28 14:38:41","admin","1","2","http:\/\/www.example.com\/members\/admin\/","http:\/\/www.example.com\/wp-content\/uploads\/avatars\/1\/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg","admin"],
[0.749063670412,"2011-09-28 14:43:11","admin","1","3","http:\/\/www.example.com\/members\/admin\/","http:\/\/www.example.com\/wp-content\/uploads\/avatars\/1\/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg","admin"],
[36.329588015,"2011-10-06 14:15:12","admin","1","10","http:\/\/www.example.com\/members\/admin\/","http:\/\/www.example.com\/wp-content\/uploads\/avatars\/1\/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg","admin"]]}
However I would like to add a name/tag to each individual piece of data e.g. 'leftPercent', 'timestamp', 'username' etc.
This is the relevant part of my script:
First I get the data from the database...
$sQuery = "
select cp_comments.*,users.user_login, users.user_url, users.display_name, users.ID as avatar
from ".$wpdb->prefix."cp_comments cp_comments
left join ".$wpdb->prefix."users users on users.ID=cp_comments.uid
where songid='$id'
order by cp_comments.id asc
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
Then I do some custom formatting on the data. (There will be other items added later, for now it is just comments.
$output = array(
"comments" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "playtime" )
{
/* Special output formatting for 'playtime' column */
$row[] = ($aRow[ $aColumns[$i] ]) / $duration * 100;
}
else if ( $aColumns[$i] == "avatar" )
{
/* Special output to render Avatar by user id */
$row[] = commentplayer_get_user_avatar($aRow[ $aColumns[$i] ]);
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['comments'][] = $row;
}
And finally I encode the JSON:
echo json_encode($output);
What part am I missing? I'm sure this is very simple. Thanks all.
If the $aColumns[$i] corresponds to the name column from a database query, then try this
$output = array(
"comments" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "playtime" )
{
/* Special output formatting for 'playtime' column */
$row[$aColumns[$i]] = ($aRow[ $aColumns[$i] ]) / $duration * 100;
}
else if ( $aColumns[$i] == "avatar" )
{
/* Special output to render Avatar by user id */
$row[$aColumns[$i]] = commentplayer_get_user_avatar($aRow[ $aColumns[$i] ]);
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[$aColumns[$i]] = $aRow[ $aColumns[$i] ];
}
}
$output['comments'][] = $row;
}
I changed $row[] to $row[$aColumns[$i]]

Categories