[wp-trac] [WordPress Trac] #51207: wpdb query returns incorrect value for update on mysql

WordPress Trac noreply at wordpress.org
Tue Sep 1 18:50:06 UTC 2020


#51207: wpdb query returns incorrect value for update on mysql
--------------------------+-----------------------------
 Reporter:  eadumbire     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Database      |    Version:  5.5
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 According to the docblock for query() function in wp-db.php , the expected
 return value should be...

 {{{
 @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP
 queries. Number of rows affected/selected for all other queries. Boolean
 false on error.
 }}}



 However, when evaluating the code for actual behavior, the wpdb->query
 function returns -1 during an UPDATE, if no rows are updated, even if
 there is no error.

 One way to replicate this is, using a fully featured modern IDE:
 1) set a conditional break-point on the first line of wpdb->query
 function, to catch any update operation. I used the condition below:
 (‌‌strpos($query, 'UPDATE') === 0)

 2) With live-debugging enabled for your WordPress website, visit the edit
 page for any post and save/publish without making any changes.

 3) Step through the code to reach the line where the return value is set.(
 line 1978-1984 on Worpdress 5.5 )

 {{{
 // Return number of rows affected.
         $return_val = $this->rows_affected;
 }}}

 4) note the value ( for me it's consistently -1, provided that the update
 is performed without altering any data.


 This bug makes it difficulty to rely on the returned value of the wpdb
 function on update operations, if we want to apply any error messaging or
 handling logic.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51207>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list