1

I am trying to connect to the #__contact_details db and get some info from there for a custom module.

public function getContact(Registry $params, SiteApplication $app)
{
    
    $db = Factory::getContainer()->get('DatabaseDriver');
    
    $cid   = (int) $params->get('main_contact');

    $query = $db->getQuery(true);

    $query
        ->select($db->quoteName(['name', 'con_position', 'address', 'suburb']))
        ->from($db->quoteName('#__contact_details'))
        ->where($db->quoteName(('id') . ' = '. $cid));


    $db->setQuery($query);
       
    
    return $list = $db->loadObject();
}

it gives Unknown column 'id = 1' in 'where clause' error. There is a column id in the db. How do i correct this? Thank you so much.

1 Answer 1

1

I had a problem with excessive parentheses. I shouldn't have passed my entire conditional expression into quoteName().

$db->quoteName(('id') . ' = '. $cid)
#             ||----| irrelevant
#             |--------------------| bad

Only the column name should be quoted as an identifier:

$db->quoteName('id') . ' = '. $cid

Working script:

public function getContact(Registry $params, SiteApplication $app)
{
    $db = Factory::getContainer()->get('DatabaseDriver');
    $cid = $params->get('main_contact');
    $query = $db->getQuery(true);
    $query
        ->select($db->quoteName(['name', 'con_position', 'address', 'suburb']))
        ->from($db->quoteName('#__contact_details'))
        ->where($db->quoteName('id') . ' = '. (int)$cid);
    $db->setQuery($query);
    return $db->loadObject();
}
1
  • Please accept your answer to show that issue has been resolved. Thank you for taking the time to come back and share your solution.
    – Irata
    Commented Nov 6 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.