XF 2.1 Correct way to use $db->update

AndyB

Well-known member
I would like o change the following query:

PHP:
$db->query("
    UPDATE xf_user
    SET message_count = message_count + 1
    WHERE user_id = ?
", $newUserId);

to a XenForo query
PHP:
$db->update('xf_user',
    ['message_count' => message_count - 1],
    'user_id = ?
        AND message_count > ?',
    [$oldUserId, 0]
);

but I get the following error message:

205074

What is the correct syntax?

Thank you.
 
For this you want to keep using $db->query. $db->update uses a bind for the sql values so you can't use it for this. That method isn't meant to be used for increment or similar.

XenForo, in their core code, also use $db->query for stuff like this. For example, at \XF\Repository\Tag lines 435-440:

PHP:
$db->query("
    UPDATE xf_tag
    SET use_count = use_count + 1,
        last_use_date = ?
    WHERE tag_id = ?
", [\XF::$time, $addId]);
 
Top Bottom