I don't think it is very direct to filter the result set when sql is perfectly qualified to do such logic.
As Lodder corrected, you also need to use setQuery()
, fix your missing $
, adjust your result set generating method call, and improve your redirect technique.
I've included some helpful diagnostic lines to help you to understand the query that is being generated and if there are any syntax errors, they will be displayed (so long as you don't run the redirect portion of your script).
After running my script, you may discover that:
- you have an sql error (for instance
userid
is not the same asuser_id
) - you don't actually have the
$joomlausers->id
value that you think you have - ...or something else that you haven't posted in your question is causing trouble.
Untested Snippet:
$app = JFactory::getApplication();
$user = JFactory::getUser();
if ($user->guest) {
JFactory::getApplication()$app->redirect(JRoute::_('index.php?option=com_something&view=something'));
}
try {
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select("COUNT(1)")
->from("#__rbid_users")
->where("userid = " . (int)$user->id);
JFactory::getApplication()$app->enqueueMessage($query->dump(), 'info');
// ^^^^^^^^^^^^^^ - use only during debuggin -^^^^^^^^^^^^^^
$db->setQuery($query);
if (!$db->loadResult()) { // loadResult() should return either 0 or 1
// no error and no count, execute redirect
JFactory::getApplication()$app->redirect(JRoute::_('index.php?option=com_something&view=something'));
} else {
// id matched
}
} catch (Exception $e) {
JFactory::getApplication()$app->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');
// use only during debugging -^^^^^^^^^^^^^^^^
}
It is important that I state that you must never show the exact query or error messages to the public as a matter of site security!
To be clear, the enqueueMessage()
calls are not required, they are just to assist your debugging.