PHP Joomla! 6 Joomla! 5 Joomla! 3 (Legacy) Database Basic

Run Secure SELECT Queries with Prepared Statements (bind and ParameterType)

Queries the database via DatabaseInterface, quoteName() and typed bound parameters to eliminate SQL injection.

Updated on 24 November 2026 75 copies 254 views
components/com_example/src/Model/ItemsModel.php
<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;

$db    = Factory::getContainer()->get(DatabaseInterface::class);
$catId = 10;
$state = 1;

$query = $db->getQuery(true)
    ->select([$db->quoteName('id'), $db->quoteName('title'), $db->quoteName('alias')])
    ->from($db->quoteName('#__content'))
    ->where($db->quoteName('catid') . ' = :catid')
    ->where($db->quoteName('state') . ' = :state')
    ->bind(':catid', $catId, ParameterType::INTEGER)
    ->bind(':state', $state, ParameterType::INTEGER)
    ->order($db->quoteName('created') . ' DESC');

$rows = $db->setQuery($query, 0, 10)->loadObjectList();
Prevents SQL injection natively via parameterized statements.
components/com_example/src/Model/ItemsModel.php
<?php
$db    = JFactory::getDbo();
$query = $db->getQuery(true)
    ->select($db->quoteName(array('id', 'title', 'alias')))
    ->from($db->quoteName('#__content'))
    ->where($db->quoteName('catid') . ' = ' . (int) $catId)
    ->where($db->quoteName('state') . ' = 1');

$rows = $db->setQuery($query, 0, 10)->loadObjectList();
Legacy Joomla 3 syntax using JFactory::getDbo().

Technical explanation and best practices

In Joomla 5 and 6, obtain the database service via DatabaseInterface::class (or $this->getDatabase() inside models) and always bind dynamic values with ParameterType.

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.