We help IT Professionals succeed at work.

AJAX Response When Query Returns Empty Data

Opeyemi AbdulRasheed
Last Modified: 2018-12-09
Hello Experts!

I normally populate DataTable with the following combination. When a button is clicked, the table is populated through AJAX call as long as there's at least 1 row of data. But if there're no data in the table from MySQL, nothing happens. So, I want a message to return when there's no data coming from database.

Please help me manipulate this:


require_once '../.../db_connection.php';

$stmt = $conn->prepare( "SELECT ... FROM ... WHERE ..." );
$stmt->bind_param( "ssssi", $_POST['session'], $_POST['term'], $_POST['class'], $_POST['subject'], $_POST['limit'] );

$assessment = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
die( json_encode($assessment) );

Open in new window

Here is my AJAX:
$('#loadStudents').click(function() {
   method : 'post',
   url : '../data/ass1_Data',
   data : { session : $('#session').val(), term : $('#term').val(), class : $('#c_taught').val(), subject : $('#s_taught').val(), limit : $('#process_limit').val(), },
   dataType : 'json',
   }).done(function(data) {

Open in new window

Thank you in advance.
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019
This one is on us!
(Get your first solution completely free - no credit card required)
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Try to add the emptyTable option to the definition of your datatable object :

var myDt = $('#my-datatable').DataTable( {
    "language": {
        "emptyTable":     "There no row message."
} );

Open in new window

@Julian Hansen, thank you sir, but there's syntax error. The IF under .done(function(data) { shows a wrong color code in my Sublime Text, which means syntax error. Kindly check, sir.

@Zakaria Acharki, thank you for your help, sir. There's already a "No data available in table" as built-in message for emptyTable. I just want an action response after clicking on Load Button.
@@Julian Hansen, forgive me sir. I copied pasted your code (CAPITAL If). Lazy me. I figured it out after sending the previous comment. It's working perfectly, sir.
Thanks both
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.