inc() and dec()

The inc() and dec() methods in decMuc/PDOdb return SQL-compatible expressions that let you increment or decrement numeric fields in a safe and atomic way. These expressions are designed for use inside update() statements and are automatically processed as raw fragments.

Signatures:

public function inc(int|float $num = 1): array<string, string>
public function dec(int|float $num = 1): array<string, string>

Incrementing a Counter

Increase the views column by 1:

$db->update('articles', ['views' => $db->inc()]);

Decreasing a Stock Value

Subtract 2 from stock:

$db->update('products', ['stock' => $db->dec(2)]);

How It Works

  • Returns an array in the form ['[I]' => 'column + 1']
  • The placeholder [I] is automatically replaced by the proper column during update()
  • Prepared statements are respected – no direct injection risk
  • Default increment/decrement is 1

Validation Rules

  • $num must be numeric (int or float)
  • Negative values are allowed but discouraged (use the appropriate method instead)
  • Used only inside update values – never in WHERE or SELECT
Hint: These helper functions are ideal for atomic counter updates, stock control, voting systems, or tracking events – all without requiring you to manually write SQL math expressions.