Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

$wpdb->update() runs, but the row in the database does not change

Your error

$wpdb->update() returns 0 and the row keeps its old values2 more lines it can show

Fix it fast

Most common cause, 1 of 4

0: no row matched the WHERE array, or the row already held these values

If the SELECT found nothing, correct the key or value in $where (a null value there becomes an IS NULL test).

Fix
$result = $wpdb->update(
    $wpdb->prefix . 'orders',
    array( 'status' => 'paid' ),
    array( 'id' => $order_id ),
    array( '%s' ),
    array( '%d' )
);
if ( false === $result ) {
    error_log( $wpdb->last_error );
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

$wpdb->update() returns the number of rows changed, or false on error. 0 is no failure: no row matched the WHERE array, or the row already held those values. false means MySQL rejected the query, a value was too long or invalid for its column, or $data or $where was not an array. Print $wpdb->last_error and $wpdb->last_query. WordPress 7.1.

  • Applies to WordPress 7.1.5, MySQL 8.4 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Capture the return value and the two diagnostics right after the call. 0 points to cause 1; false with a message points to cause 2 or 3; false with an empty last_error points to cause 4.

Run this first
$result = $wpdb->update(
    $wpdb->prefix . 'orders',
    array( 'status' => 'paid' ),
    array( 'id' => 42 )
);
var_dump( $result, $wpdb->last_error, $wpdb->last_query );

Then find the line you see in this list and open its fix:

  1. 10: no row matched the WHERE array, or the row already held these valuesupdate() returns int(0), last_error is empty, and a SELECT with the same condition either finds nothing or finds the row with the values you tried to write
  2. 2false: MySQL rejected the querylast_error holds a MySQL message such as Table '...' doesn't exist or Unknown column; for a missing table, last_query shows the SHOW FULL COLUMNS lookup wpdb ran before the UPDATE
  3. 3false: a value is too long or invalid for its columnlast_error reads Processing the value for the following field failed, followed by the column name
  4. 4false with no message: $data or $where is not an arrayupdate() returns false, last_error is empty, and last_query shows an earlier statement instead of this UPDATE

What does the error look like?

  • $wpdb->update() returns 0 and the row keeps its old values
  • $wpdb->update() returns false
  • WordPress database error: Processing the value for the following field failed: title. The supplied value may be too long or contains invalid data.

Cause 1 of 4

0: no row matched the WHERE array, or the row already held these values

You seeupdate() returns int(0), last_error is empty, and a SELECT with the same condition either finds nothing or finds the row with the values you tried to write

Check

update() returns the number of rows changed, and 0 is a successful query that changed nothing. Either the WHERE array matched no row (each key becomes column = value, joined with AND, so one wrong value is enough), or the row already had those values: MySQL's UPDATE counts only rows actually changed and skips a column set to its current value. Code that tests if ( ! $result ) then reports a failure that never happened.

Check
var_dump( $wpdb->get_row( $wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}orders WHERE id = %d", 42
) ) );

Fix

If the SELECT found nothing, correct the key or value in $where (a null value there becomes an IS NULL test). If it found the row already up to date, treat only false as an error.

Fix
$result = $wpdb->update(
    $wpdb->prefix . 'orders',
    array( 'status' => 'paid' ),
    array( 'id' => $order_id ),
    array( '%s' ),
    array( '%d' )
);
if ( false === $result ) {
    error_log( $wpdb->last_error );
}

Verify

A changed value returns 1, saving the same values again returns 0 without an error, and the SELECT shows the new data.

Verify
var_dump( $result, $wpdb->last_error );

Confirmed by developer.wordpress.org

Did this fix it?

Cause 2 of 4

false: MySQL rejected the query

You seelast_error holds a MySQL message such as Table '...' doesn't exist or Unknown column; for a missing table, last_query shows the SHOW FULL COLUMNS lookup wpdb ran before the UPDATE

Check

A false result with a database message means the query reached MySQL and failed. A hard-coded wp_ prefix on a site whose prefix differs, a table that a plugin never created, or a key in $data that is not a column of the table all end here.

Check
echo $wpdb->prefix; echo $wpdb->last_error;

Fix

Build the table name from $wpdb->prefix and use the column names exactly as the table defines them.

Fix
$table = $wpdb->prefix . 'orders';
$cols  = $wpdb->get_col( "SHOW COLUMNS FROM {$table}" );
var_dump( $cols );

Verify

last_error is empty after the call, and the result is a number.

Verify
var_dump( $result, $wpdb->last_error );

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

false: a value is too long or invalid for its column

You seelast_error reads Processing the value for the following field failed, followed by the column name

Check

Before it sends the query, wpdb checks each value against the column's character set and length. When a value would have to be cut or cleaned to fit, it refuses the whole update and names the field, so nothing reaches MySQL.

Check
var_dump( $wpdb->get_col_length( $wpdb->prefix . 'orders', 'title' ) );

Fix

Trim or validate the value before the call, or widen the column if longer values are legitimate.

Fix
$title = mb_substr( $title, 0, 190 );
$wpdb->update( $wpdb->prefix . 'orders', array( 'title' => $title ), array( 'id' => 42 ) );

Verify

The call returns 1 and last_error stays empty.

Verify
var_dump( $result, $wpdb->last_error );

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

false with no message: $data or $where is not an array

You seeupdate() returns false, last_error is empty, and last_query shows an earlier statement instead of this UPDATE

Check

update() returns false straight away, before any query is built, when either the data or the WHERE argument is not an array. Passing a string such as "id = 42" as the WHERE clause is one way to get here.

Check
var_dump( is_array( $data ), is_array( $where ) );

Fix

Pass the condition as a column => value array. For conditions that need other operators, write the query with $wpdb->prepare() and $wpdb->query() instead.

Fix
$wpdb->update( $wpdb->prefix . 'orders', array( 'status' => 'paid' ), array( 'id' => 42 ) );

Verify

last_query now shows the UPDATE statement.

Verify
echo $wpdb->last_query;

Confirmed by developer.wordpress.org

Did this fix it?

How do I stop it coming back?

  • Check the result of update() with === false in every call site, never with a plain truthiness test.
  • Build table names from $wpdb->prefix everywhere, including in plugin activation code.
  • Validate lengths against the column definition before saving user input.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.