Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed
 asked on

AJAX Response When Query Returns Empty Data

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.

Avatar of undefined
Last Comment
Opeyemi AbdulRasheed

8/22/2022 - Mon
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Zakaria Acharki

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

Opeyemi AbdulRasheed

@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.
Opeyemi AbdulRasheed

@@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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Opeyemi AbdulRasheed

Thanks both