Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla! JDatabase::getErrorNum() is deprecated, use exception handling instead

I have the following code:

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    $query->select('*');
    $query->from('#__users');
    $db->setQuery($query);

    // Check for a database error.
    if ($db->getErrorNum()) {
        JError::raiseWarning(500, $db->getErrorMsg());
    }       

    $result = $db->loadResult();

Now getErrorNum as well as JError are deprecated.

Just to clarify, JError and $db->getErrorNum() are NOT deprecated in Joomla 2.5 but are in Joomla! 3.0. So this question has value for somebody who develops for 2.5 but wants to make an easy upgrade to 3.X series.

So with what exactly to replace them so that I can properly check for database errors?

like image 643
Valentin Despa Avatar asked Dec 20 '12 08:12

Valentin Despa


1 Answers

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select('*')
      ->from('#__users');

try
{
    $db->setQuery($query);
    $result = $db->loadResult();
}
catch (RuntimeException $e)
{
    echo $e->getMessage();
}

Hope it helps

like image 54
McRui Avatar answered Oct 24 '22 07:10

McRui