Finding the MySQL MAX number in Joomla Database

I needed to find the max number in my Joomla Database and increment this by one. Here is the code I used that ended up working for me.

// Get the highest ticket number and add 1
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(‘MAX(‘.$db->quoteName(‘number’).’)’)
->from($db->quoteName(‘ticket_num’))
->order(‘number ASC’)
->limit(‘1’);
$db->setQuery($query);
$my_value = $db->loadResult();
// Add 1
$next_num = ($my_value + 1);

I found this link that helped me figure it out.

http://stackoverflow.com/questions/21280886/mysql-max-function-does-not-work-in-joomla

I hope this helps someone else…

1 Comments

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>