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

Update Specific Columns Safely with Bound UPDATE Queries

Runs targeted SQL UPDATE statements (such as atomic counter increments or state changes) without overwriting other columns.

Updated on 24 November 2026 36 copies 134 views
components/com_example/src/Model/ItemModel.php
<?php

declare(strict_types=1);

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

$db     = Factory::getContainer()->get(DatabaseInterface::class);
$itemId = 42;
$now    = Factory::getDate()->toSql();

$query = $db->getQuery(true)
    ->update($db->quoteName('#__example_items'))
    ->set($db->quoteName('hits') . ' = ' . $db->quoteName('hits') . ' + 1')
    ->set($db->quoteName('modified') . ' = :now')
    ->where($db->quoteName('id') . ' = :id')
    ->bind(':now', $now, ParameterType::STRING)
    ->bind(':id', $itemId, ParameterType::INTEGER);

$db->setQuery($query)->execute();
SQL-level hits = hits + 1 is atomic under concurrent traffic.
components/com_example/src/Model/ItemModel.php
<?php
$db    = JFactory::getDbo();
$query = $db->getQuery(true)
    ->update($db->quoteName('#__example_items'))
    ->set($db->quoteName('hits') . ' = ' . $db->quoteName('hits') . ' + 1')
    ->where($db->quoteName('id') . ' = ' . (int) $itemId);

$db->setQuery($query)->execute();
Legacy Joomla 3 version via JFactory::getDbo().

Technical explanation and best practices

Ideal for atomic view/copy counters or status transitions without race conditions from loading and saving entire table rows.

Direct Engineering

Have a Joomla project or technical challenge in mind?

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