Server side search error

Server side search error

javaprogjavaprog Posts: 7Questions: 0Answers: 0
edited August 2010 in General
I have implemented server side php-postgres script and it is working with the exception of the dynamic search field. Everytime I start to type something in the field, a message pops up giving me this error:

DataTables warning: JSON data from server failed to load or be parsed. This is most likely to be caused by a JSON formatting error.

Help please..

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    edited August 2010
    As it says, there is probably a JSON error. Can you post your JSON data here please so we can see what might be wrong with it - it's possible I've missed an escape in the script or something... It's also quite possible that there is a PHP error such as 'cannot connect to database' which will be killing it.

    Allan
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    Here is my code:

    <?php



    $aColumns = array( 'item_id', 'batch_id', 'client_id', 'debit_amt', 'item_status' );

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "item_id";

    /* DB table to use */
    $sTable = "item";

    /* Database connection information */
    $gaSql['user'] = "";
    $gaSql['password'] = "";
    $gaSql['db'] = "";
    $gaSql['server'] = "";



    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * 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 ".pg_escape_string( $_GET['iDisplayLength'] )." OFFSET ".
    pg_escape_string( $_GET['iDisplayStart'] );
    }


    /*
    * Ordering
    */
    if ( isset( $_GET['iSortCol_0'] ) )
    {
    $sOrder = "ORDER BY ";
    for ( $i=0 ; $i
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Can you post your JSON data here please so we can see what might be wrong with it?

    Thanks,
    Allan
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    Here is my json data, which I also ran against the json lint validator. The validator said it was valid. So here it is:

    {
    "sEcho": 0,
    "iTotalRecords": 21883,
    "iTotalDisplayRecords": 21883,
    "aaData": [
    [
    "168",
    "1",
    "1000000008",
    "10",
    "338.00"
    ],
    [
    "2362",
    "1",
    "1000000134",
    "10",
    "0.00"
    ],
    [
    "169",
    "1",
    "1000000008",
    "10",
    "332.00"
    ],
    [
    "170",
    "1",
    "1000000008",
    "10",
    "167.00"
    ],
    [
    "171",
    "1",
    "1000000008",
    "10",
    "382.00"
    ],
    [
    "172",
    "1",
    "1000000008",
    "10",
    "363.00"
    ],
    [
    "173",
    "1",
    "1000000008",
    "10",
    "325.00"
    ],
    [
    "2363",
    "1",
    "1000000134",
    "10",
    "0.00"
    ],
    [
    "2365",
    "1",
    "1000000134",
    "10",
    "0.00"
    ],
    [
    "166",
    "1",
    "1000000008",
    "10",
    "360.00"
    ]
    ]
    }
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    I also reset my php postgres code again which I will list below. I will also post my html page below that for you so you will have all code that I am using.

    <?php
    /*
    * Script: DataTables server-side script for PHP and PostgreSQL
    * Copyright: 2010 - Allan Jardine
    * License: GPL v2 or BSD (3-point)
    */

    */
    $aColumns = array( 'item_id', 'client_id', 'batch_id', 'item_status', 'debit_amt' );

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "item_id";

    /* DB table to use */
    $sTable = "item";

    /* Database connection information */
    $gaSql['user'] = "";
    $gaSql['password'] = "";
    $gaSql['db'] = "";
    $gaSql['server'] = "";




    /*
    * 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 ".pg_escape_string( $_GET['iDisplayLength'] )." OFFSET ".
    pg_escape_string( $_GET['iDisplayStart'] );
    }


    /*
    * Ordering
    */
    if ( isset( $_GET['iSortCol_0'] ) )
    {
    $sOrder = "ORDER BY ";
    for ( $i=0 ; $i
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    And here is the html code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">





    DataTables example

    @import "../../media/css/demo_page.css";
    @import "../../media/css/demo_table.css";




    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../examples_support/server_processing.php"
    } );
    } );





    DataTables server-side processing example


    Preamble

    Live example




    Rendering engine
    Browser
    Platform(s)
    Engine version
    CSS grade




    Loading data from server




    Rendering engine
    Browser
    Platform(s)
    Engine version
    CSS grade






    Please refer to the DataTables documentation for full information about its API properties and methods.



    DataTables © Allan Jardine 2008-2010
  • someansomean Posts: 4Questions: 0Answers: 0
    edited August 2010
    dude, try get rid of your

    "pg_escape_string"


    ie. you wrote:

    $sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";

    try:

    $sWhere .= $aColumns[$i]." ILIKE '%" . $_GET['sSearch'] . "%' OR ";
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    Just tried that. Still does not work. Still getting same error. Any other ideas? I am all out.
  • christineb1979christineb1979 Posts: 1Questions: 0Answers: 0
    Yeah its not work for me also
  • someansomean Posts: 4Questions: 0Answers: 0
    maybe try using LIKE instead of ILIKE?

    The json data you posted is it the data you obtained by calling the server-side script with a search keyword? i.e. by calling it with sSearch=somthing?
  • javaprogjavaprog Posts: 7Questions: 0Answers: 0
    Still not working...surely someone has gotten this code to work with a postgres server, otherwise, why would it be available for download?
  • Kode3Kode3 Posts: 5Questions: 0Answers: 0
    edited August 2010
    I am not sure this is going to help since I use MySQL but just in case I will put what I have learned.

    When I was using the example code for server-side I had a similar problem and it all has to do with the escape string.

    You see, JSON was escaping my escape characters and therefore when it got to be time to process it it would fail.

    So try this, remove all the (not sure if this will be necessary)

    [code]
    pg_escape_string()
    [/code]

    and get rid of (fairly sure this should help)

    [code]
    $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
    [/code]

    and see if that will work.

    Also Allan, your while statement in the example should look like this:
    [code]
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $sOutput .= "[";
    $sOutput .= '"'.$aRow['engine'].'",';
    $sOutput .= '"'.$aRow['browser'].'",';
    $sOutput .= '"'.$aRow['platform'].'",';
    if ( $aRow['version'] == "0" )
    $sOutput .= '"-",';
    else
    $sOutput .= '"'.$aRow['version'].'",';
    $sOutput .= '"'.$aRow['grade'].'"';
    $sOutput .= "],";
    }
    [/code]

    I have tested this and it works for me.
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    I've been using the script available here ( http://datatables.net/development/server-side/php_postgres ) no problem with my database.

    Going back to javaprog's JSON output - I'd say the problem is almost certainly the sEcho: 0. It should never be zero, and the table won't render if it is. It should start at one, and increment by one on every draw. It needs to pass back the value that DataTables sends it as sEcho - which should be this line:

    [code]
    $sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
    [/code]
    javaprog can you give us a link to this example please? If not, then can you check that the sEcho value is going up by one for each draw when sending and receiving.

    Allan
  • sporkd2sporkd2 Posts: 5Questions: 0Answers: 0
    Hey guys,

    This is the exact problem I am having now and was wondering if there was ever a fix for this?

    Thanks
  • sporkd2sporkd2 Posts: 5Questions: 0Answers: 0
    BTW,

    I am using exaclty this, whenever I type anything in the search box, I get the JSON error message.

    http://datatables.net/development/server-side/php_postgres
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    What is your JSON return from the server? If you have a look at the XHR in Firebug you'll be able to see what the server is returning.

    Allan
  • sporkd2sporkd2 Posts: 5Questions: 0Answers: 0
    FYI,

    Seems that the basic config script doesnt take into account if you are using integers since its going to try to do a LIKE 'integer' which wont work. As soon as I took out the integer field everything worked fine.
This discussion has been closed.