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

吃可爱长大的小学妹 提交于 2019-12-04 19:15:46

问题


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?


回答1:


$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




回答2:


JError is NOT deprecated in Joomla 2.5 - but in Joomla 3.0 (Platform 12.1 onwards) so for Joomla 2.5 this is not an issue. JError is being replaced with the php exceptions (link to the php guide here)

There is also a Joomla forum question about this here

Using exception is also the case for getErrorNum() - again read the php documentation link above for more info. There's a nice mysql example on the page here.




回答3:


$app = JFactory::getApplication();

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

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

try
{
    $db->setQuery($query);
    $result = $db->loadObjectList();
}
catch (Exception $e)
{
    $app->enqueueMessage(JText::_($e->getMessage()), 'error');
}

and redirect to your url. Error message will be displayed



来源:https://stackoverflow.com/questions/13967904/joomla-jdatabasegeterrornum-is-deprecated-use-exception-handling-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!