Trongate Docs
switch to dark modeswitch to dark mode
The 'Get Max' Method

The 'Get Max' Method

The 'Get Max' method returns the maximum 'id' value from a target database table. It can accept only one argument - an optional table name.

  • $target_table (optional) - the name of the database table to be queried. By default, the Model will assume the target table to be equal to the value on the first URL segment.

What Gets Returned?


If matching records are found then an integer, representing the maximum 'id' value stored on the target, table will be returned.


If the query fails to find any matching records then a NULL value will be returned.

Example 1

The syntax below shows the simplest Get Max example possible.  Here the method would be assuming the first segment of the URL to be the table name.

$max_id = $this->model->get_max();

The code above will produce the following SQL query:

SELECT MAX(id) AS max_id FROM tablename

Just To Let You Know
In a working example, 'tablename' would be replaced with the first segment from the URL.

Below is an example of a Members.php controller file that contains a test() method.  Here we're invoking a basic 'Get Max' command and then immediately displaying the results using Trongate's json() method:

class Members extends Trongate {

    function test() {
        $max_id = $this->model->get_max();
        echo $max_id;

Below shows an example of the kind of output that we can expect to see from the above method, when Debug Mode is switched on:


Example 2

Below is a more advanced example:

$max_id = $this->model->get_max('fish');

The code above would produce the following SQL query:

SELECT MAX(id) AS max_id FROM fish


If you have a question or a comment relating to anything you've see here, please goto the Help Bar.