Skip to content
Advertisement

Fetch specific rows from MySQL with datatables

I’m trying to fetch specific rows in a MySQL table with specific “basket_id”s and display them in the jQuery DataTables plugin.

In the PHP server-side script for the DataTables AJAX request, I specify the $user->basketId() in a WHERE clause for MySQL..

Let me walk you through it..

  1. User signs in.
  2. USER class stores user session data. (use public methods to retrieve data)
  3. In dashboard user clicks ITEMS and sees a datatable.

The problem is that all users are seeing all items from all baskets.

I have verified that the $user->basketId() method return the correct basket ID for the user.

JavaScript

I’m not sure why, but it doesn’t matter which user I am logged into and which basket ID is returned, datatables always fetches all the items in the items table.

Advertisement

Answer

So, after doing some researching using all the keywords I could think of I came up with an answer.

DataTables already provides an SSP.php library which is a Server-side Processing Class for PHP.

Using the SSP::complex() method we can define two extra conditions:

JavaScript
  1. $whereResult=null

This is a WHERE clause for data AFTER it has been fetched from the DB table, so this will filter through results after the SQL results are returned.

  1. $whereAll=null

This is a WHERE clause for data BEFORE it has been fetched from the DB, so this will add a WHERE condition before the SQL query and will only return results that match.

After everything my final get_items.php AJAX file looks like

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement