Please help with MySQL integration

Please help with MySQL integration

smcac11smcac11 Posts: 7Questions: 0Answers: 0
edited July 2013 in General
Hi,

I'm trying to load information from a mysql database into datatables. When I load my html file, I'm getting the seemingly common error:

DataTables warning (table id = 'tableid'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

When I tested my json return using JSONLint, it said I had the following error, which baffles me:

Parse error on line 1:
<!DOCTYPEhtml>

Replies

  • allanallan Posts: 63,201Questions: 1Answers: 10,415 Site admin
    [code]
    Parse error on line 1:
    <!DOCTYPEhtml>
  • smcac11smcac11 Posts: 7Questions: 0Answers: 0
    edited July 2013
    Ah - ok. I'll do some more studying of JSON.

    As for the second question, I do have bServerSide processing enabled. I was unable to render my code properly using jsbin. Here is my code:

    HTML:
    [code]


    User ID


    Col1
    Col2
    Col3
    Col4
    Col5
    Col6
    Col7
    Col8
















    [/code]

    Initialization code (housed in same document with HTML above):

    [code]

    $(document).ready(function() {
    $('#tablename').dataTable( {
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "server_processing_copy_datatables.php"
    } );
    } );

    [/code]

    Server-side processing (PHP) (housed in separate document):

    [code]
    <?php
    /**
    * Script: DataTables server-side script for PHP 5.2+ and MySQL 4.1+
    * Notes: Based on a script by Allan Jardine that used the old PHP mysql_* functions.
    * Rewritten to use the newer object oriented mysqli extension.
    * Copyright: 2010 - Allan Jardine (original script)
    * 2012 - Kari Söderholm, aka Haprog (updates)
    * License: GPL v2 or BSD (3-point)
    */
    mb_internal_encoding('UTF-8');

    /**
    * 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( 'user_id', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5',
    'Col6', 'Col7', 'Col8' );

    // Indexed column (used for fast and accurate table cardinality)
    $sIndexColumn = 'user_id';

    // DB table to use
    $sTable = 'tablename';

    // Database connection information
    $gaSql['user'] = 'user';
    $gaSql['password'] = 'pass';
    $gaSql['db'] = 'tablename';
    $gaSql['server'] = 'myserver';
    $gaSql['port'] = 3306; // 3306 is the default MySQL port

    // Input method (use $_GET, $_POST or $_REQUEST)
    $input =& $_GET;

    /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is
    * no need to edit below this line
    */
    [/code]

    ... [[ONLY INCLUDING RELEVANT CODE FROM THIS SECTION]]...

    [code]
    /**
    * Output
    */
    $output = array(
    "sEcho" => intval($input['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array(),
    );
    [/code]


    I'm currently calling the document housing the HTML/JS & the document housing the PHP from a separate document. However, when I test it I get the following error :

    [quote]
    Notice: Undefined index: sEcho in ... php/datatables.inc.php on line 156
    {"sEcho":0,"iTotalRecords":"8","iTotalDisplayRecords":"8","aaData":[["1","M.A.","Anthropology","University of Minnesota","2011","10","200","2020","No."],["2","M.S.","Water Resources Management","University of Wisconsin, Madison","2011","10","150","2025","Perhaps not."],["5","","mba","","2013","150","0","0",""],["6","","mba","","2013","150","0","0",""],["7","","","","0","","","0",""],["8","","","","0","","","0",""],["9","","","","0","","","0",""],["10","","adf","","2343","234000","","0",""]]}
    [/quote]

    It also says [quote]processing[/quote] over my table when I test the page. Any idea what I may be doing wrong?
  • smcac11smcac11 Posts: 7Questions: 0Answers: 0
    edited July 2013
    [deleted comment]
  • allanallan Posts: 63,201Questions: 1Answers: 10,415 Site admin
    I must admits, I'm not sure why that isn't working - it looks like it should (although I can't see where it is actually using sEcho so there might be something funny going on there).

    Where did you get that particular PHP script from?

    Have you tried this one: http://datatables.net/development/server-side/php_mysql

    Allan
  • smcac11smcac11 Posts: 7Questions: 0Answers: 0
    This is the script I was using: http://datatables.net/development/server-side/php_mysqli.

    I tried the one you recommended above, but unfortunately it resulted in the same problem.

    One bit of good news: I'm no longer getting the JSON formatting error. As for why, I'm not sure. But no longer getting it running either PHP script.
  • allanallan Posts: 63,201Questions: 1Answers: 10,415 Site admin
    Can you run the debugger over the table please?

    Thanks,
    Allan
  • smcac11smcac11 Posts: 7Questions: 0Answers: 0
    Just sent you the code.
  • allanallan Posts: 63,201Questions: 1Answers: 10,415 Site admin
    Replied in the PM.
This discussion has been closed.