I have a Joomla 3.x module which calls the category name from the database and then shows this in the module position.
It does this with:
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$view = JRequest::getString('view');
if ($view == 'category') {
$sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
} else {
$sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
}
$db->setQuery($sql);
$category = $db->loadResult();
echo '<h1>' . $category . '</h1>';
This works in Joomla 3, but in Joomla 4 I'm getting an error:
Attempted to load class "JRequest" from the global namespace.
What's the best way to get this working in Joomla 4?
bind()
calls with every query that uses variables for values. When not joining tables, you don't need to bloat your SQL adding the table name before every column.