使用jQuery从MySQL数据库中获取数据到数据表中

问题描述:

我已经开发了一个mysql数据库和一个php代码。在PHP代码中,我使用jQuery(ajax调用)从数据库中获取数据。在html文件中,我仅打印了数据表的表头。我想从数据库中获取其余数据。代码给出:

I have developed a mysql database and a php code. In php code i am using jQuery (ajax call) to fetch the data from the database. In html file i have printed datatable's table head only. Rest data i want to fetch from the database. code is given:

HTML代码:

<div class="container box">
     <div class="table-responsive">
            <div id="alert_message"></div>
            <table id="example" class="display">
               <thead>
                  <tr>
                     <th>Student ID</th>
                     <th>Student Name</th>
                     <th>Email ID</th>
                     <th>Mobile</th>
                     <th>Status</th>
                  </tr>
               </thead>
            </table>
         </div>
      </div>






jQuery代码:

<script>
    $(document).ready(function() {
        $('#example').dataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "fetch.php",
                "type": "GET",
                "datatype": "json"
            }
        });
    });
</script>






fetch.php

<?php
$connect = mysqli_connect("localhost","root","","lib");
$sql = "SELECT StudentId, FullName, EmailId, MobileNumber, Status  FROM tblstudents";
$result = mysqli_query($connect,$sql);

$json_array = array();

while ($row = mysqli_fetch_assoc($result)) 
{
    $json_array[] = $row;
}

echo json_encode($json_array);
?>






仍然没有将数据打印在数据表中。


Still the data is not printed in the datatable. What changes are necessary in jQuery?

我知道这确实很晚,但是我使用了与您完全相同的代码(谢谢)顺便说一句!),对我来说起作用的是简单地添加到JQuery代码中:

I know this is really late, but I used the exact same code as you (thanks for that by the way!), and what made it work for me is to simply add to the JQuery code:

dataSrc =(在url ='之后。 。')

dataSrc = '' (after url = '...')

,以便DataTables知道它正在加载数组。放进去使代码工作正常!

so that DataTables knows that it's loading an array. Putting that in made the code work fine!