Datatables - Json output - PostgreSQL - Returns nu

2019-08-25 04:50发布

问题:

I use datatables. I use it in conjunction with a Postgresql server. I want to take the data from the Postgresql server. I use the script found on: http://datatables.net/development/server-side/php_postgres

The script works and creates the json file. The problem is that the values on the json file are null.

This is what it returns:

{"sEcho":1, "iTotalRecords":4, "iTotalDisplayRecords":4, "aaData":[[null,null,null], [null,null,null], [null,null,null], [null,null,null]]}

Also what i don't understand is the variable $sIndexColumn (Indexed column (used for fast and accurate table cardinality)). I have set its value to the first column of my table e.g. $sIndexColumn = "'Name'";. Is this the correct use ?

Thanks in advance.

回答1:

The documentation says:

To use the code on your own server, simply change the $aColumns array to list the columns you wish to include from your database, set $sIndexColumn to a column which is indexed (for speed), $sTable to the table name, and finally fill in your database connection parameters to $gaSql.

Emphasis mine. So, $sIndexColumn should be a column name, not a quoted string. Try this:

$sIndexColumn = "Name";

Single quotes are used for strings in PostgreSQL (and most other flavors of SQL).

I'm guessing that you made the same quoting problem with your $aColumns, i.e. you did something like this:

$aColumns = array("'One'", "'Two'", "'Three'");

when you should have done something like this:

$aColumns = array("One", "Two", "Three");

You're getting three columns out but there's nothing in those columns and those column values come from here:

$row[] = $aRow[ $aColumns[$i] ];

So if $aColumns is wrong then you'll get the nulls that you're seeing.