【问题标题】:Cannot read property of NULL in Jquery Datatables无法在 Jquery 数据表中读取 NULL 的属性
【发布时间】:2014-10-13 15:28:33
【问题描述】:

我是 Satya,试图从数据库中获取数据并在 Jquery 数据表中显示数据。

我在下面包含了我的代码。这里的场景是我试图在没有任何 where 子句的情况下获取 sql 查询数据。我能够在数据表中显示数据,当我包含它显示的 where 子句时:Cannot read property error of null,我尝试了很多来解决这个问题,即使我通过 ajax 将数据从 php 获取到 javascript 页面,但它没有将数据获取到数据表,这是我的代码:

jquery:

$("<table id='example' class='display' cellspacing='0' width='100%' style:'text-align:center;'>"
    +"<thead>"
    +"<tr>"
    +"<th>EID</th>"
    +"<th>EMICH_EMAIL</th>"
    +"<th>FIRSTNAME</th>"
    +"<th>LASTNAME</th>"
    +"<th>SIGN_IN</th>"
    +"<th>SIGN_OUT</th>"
    +"<th>DURATION</th>"
    +"</tr>"
    +"</thead>"
    +"<tbody>").appendTo('#table-section');
    $('#example').dataTable({           
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
                   "sSwfPath":"http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
        },         
        "bProcessing": false,
        "sAjaxDataProp":'',
        "sAjaxSource":"fetchdata.php"
    });

PHP:

<?php
    include 'dataconnection.php';

    $user_id=$_POST['eid'];
    $from=$_POST['startdate'];
    $to=$_POST['enddate'];

    $students_data="select st.EID,st.emich_email,firstname,lastname,sign_in,sign_out, " +
    "SEC_TO_TIME(TIME_TO_SEC(timediff(sign_out,sign_in))) AS totalduration from "+
    "student_details st INNER JOIN scans sc ON st.emich_email = sc.emich_email where st.EID ='$user_id'";

    $students_data_query=mysqli_query($connection,$students_data);

if($students_data_query){
    while($row = mysqli_fetch_array($students_data_query)){

    $output[]=array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6]);
}
echo json_encode($output);
//echo $user_id;
}else
echo die("The error is:".mysqli_error($connection));
?>

【问题讨论】:

  • 需要对NULL值进行处理​​。在您的查询中,输入IFNULL() 函数。
  • 您好 Fernando 我的数据中没有任何 NUll 值,即使从数据库中获取数据后,我只是在屏幕上提醒它们,我没有找到任何空值,但数据没有得到显示在数据表中。非常感谢您抽出时间来回答我的问题。
  • 嗯...我明白了.. JSON 输出的格式是什么?
  • 您好费尔南多,我正在通过以下方式获得,[["银行存款","12345.4567","3456.90","3456.435"],["旅行","98712.67","2458.90 ","3981.25"]],这里每块值都是数据表中的单行

标签: php ajax datatables jquery-datatables


【解决方案1】:

Karthik,试试这个。对我来说,它工作得很好。

<?php
include 'dataconnection.php';

$user_id = $_POST ['eid'];
$from = $_POST ['startdate'];
$to = $_POST ['enddate'];

if (isset ( $_POST ['eid'] )) {
    // Descrição se $_POST['truc'] existe

    /* Table Columns */
    $aColumns = array (
            'EID',
            'EMICH_EMAIL',
            'FIRSTNAME',
            'LASTNAME',
            'SIGN_IN',
            'SIGN_OUT',
            'TOTALDURATION' 
    );

    $students_data = "select st.EID,st.emich_email,firstname,lastname,sign_in,sign_out, " + 
        "SEC_TO_TIME(TIME_TO_SEC(timediff(sign_out,sign_in))) AS totalduration from " + 
        "student_details st INNER JOIN scans sc ON st.emich_email = sc.emich_email where st.EID ='$user_id'";

    $students_data_query = mysqli_query ( $connection, $students_data );

    $output = array (
            "aaData" => array () 
    );

    if ($students_data_query) {
        $row = array ();
        while ( $aRow = mysqli_fetch_array ( $students_data_query ) ) {
            for($i = 0; $i < count ( $aColumns ); $i ++) {
                if ($aColumns [$i] != ' ') {
                    // Charset for accentuation.
                    $row [] = iconv ( "iso-8859-1", "utf-8", $aRow [$aColumns [$i]] );
                }
            }
            $output ['aaData'] [] = $row;
        }
        echo json_encode ( $output );
    } else
        echo die ( "The error is:" . mysqli_error ( $connection ) );
} else {
    echo die ( "The error is: S_POST [eid] IS NULL" );
}
?>

jquery:

$("<table id='example' class='display' cellspacing='0' width='100%' style:'text-align:center;'>"
    +"<thead>"
    +"<tr>"
    +"<th>EID</th>"
    +"<th>EMICH_EMAIL</th>"
    +"<th>FIRSTNAME</th>"
    +"<th>LASTNAME</th>"
    +"<th>SIGN_IN</th>"
    +"<th>SIGN_OUT</th>"
    +"<th>DURATION</th>"
    +"</tr>"
    +"</thead>"
    +"<tbody>").appendTo('#table-section');
    $('#example').dataTable({
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
                   "sSwfPath":"http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
        },         
        "bProcessing": false,
        "sAjaxDataProp":'',
        "sAjaxSource":"fetchdata.php",
        "aoColumns" : [
                    {
                        "sTitle": "EID",
                        "aTargets": [0]
                    },     
                    {
                        "sTitle": "EMICH_EMAIL",
                        "aTargets": [1]
                    },
                    {
                        "sTitle": "FIRSTNAME",
                        "aTargets": [2]
                    },
                    {
                        "sTitle": "LASTNAME",
                        "aTargets": [3]
                    },
                    {
                        "sTitle": "SIGN_IN",
                        "aTargets": [4]
                    },
                    {
                        "sTitle": "SIGN_OUT",
                        "aTargets": [5]
                    },
                    {
                        "sTitle": "DURATION",
                        "aTargets": [6]
                    }]
    });

【讨论】:

  • 是的,给我几分钟,我会告诉你费尔南多。
  • 或者根据这个例子试试。和你的情况类似。 POST data - Server Side
  • 你好费尔南多,我只是想知道你在代码中哪里声明了 $aRow,即使我收到了警告。
  • 你能给我 30 分钟的时间吗,我现在要开会,我会试试的,让你知道,非常感谢 fernando 抽出时间,我是认真的
  • 你好费尔南多,aColumns 数组值是否应该与数据库列中区分大小写的匹配,或者是用户定义的,因为我在 iconv 语句中收到错误,指出 eid 是未定义的索引。
猜你喜欢
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多