0

When I try to retrieve data from my database to the table, I get the below error:

DataTables warning (table id = 'myTable'): Requested unknown parameter '0' from the data source for row 1

Below is the js that I used

<script>
    $(document).ready(function() {
        $('#myTable').dataTable();
    });
    </script>

Below is my table

<table id="myTable" class="table table-striped table-bordered table-hover table-condensed">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Date</th>
                            <th>Options</th>
                        </tr>
                    </thead>
                    <tbody id="myTableOK">
                         <?php echo $tableQuery ?>
                    </tbody>
                </table>

php Code

function tableQuery($linkDB){

$out = '';

$query = $linkDB -> query("SELECT id,name,date
                              FROM tbl_mytable ORDER BY name ASC");

if($query -> num_rows != 0){


    while($listOK = $query -> fetch_assoc())
    {
        $out .= '
            <tr>
                <td>'.$listOK ['name'].'</td>
                <td>'.$listOK ['date'].'</td>
                <td class="centerTXT"><a data-action="edit" class="btn btn-xs" href="'.$listOK ['id'].'">Edit</a> <a data-accion="delete" class="btn btn-xs" href="'.$listOK ['id'].'">delete</a></td>
            <tr>
        ';
    }

}
else{
    $out = '
        <tr id="noData">
            <td colspan="5" class="centerTXT">DATABASE WITHOUT DATA</td>
        </tr>
    ';
}
return $out;
}

I'm using DataTables.

Can someone tell me why am I getting that error and how to retrieve the data to the table?

It is because I'm using PHP to dynamically show the data records of the database?

Thank you.

pesk1
  • 3
  • 2

2 Answers2

0

In your script you have echo $tableQuery, but tableQuery is a function. Try this:

<?php echo tableQuery(); ?>
Kryten
  • 15,230
  • 6
  • 45
  • 68
  • tableQuery is a function, but it returns $out that is the html code of the 's of the table. That is not the problem. – pesk1 Apr 08 '14 at 16:58
  • And how are you echoing `$out` to the browser? There are two problems: 1. you're not referring to the function at all, you're referring to a variable called `$tableQuery` (which I don't see declared anywhere); and 2. even if you call the function correctly (with `tableQuery()`), you still need to `echo` the value returned by that function. If you look at the HTML source in your browser, I think you'll find your table is empty. DataTable is trying to parse the table to get some data, and there's nothing there. – Kryten Apr 08 '14 at 17:11
0

Datatables need the exact number of column in the "thead" and the "tbody", otherwise it will throw an error.

When your $query return no result, you should not return any text at all, Datatables will display "No data available in table" which you can change by passing an argument to the Datatable constructor :

<script>
    $(document).ready(function() {
         $('#myTable').dataTable(
                "oLanguage": {
                     "sEmptyTable":     "My Custom Message On Empty Table"
                 } 
         );
    });
</script>

Source : How to show empty data message in Datatables

Community
  • 1
  • 1