This error on following modules

db_affected_rows return
0 if there is no update. mean everything is same in update query.
eg: update node set status=1 where nid=1;

-1 if record does n't exists.
eg: update node set status=1 where nid=-9;

check db_affected_rows function

it call following php function

if (!db_affected_rows()) {

must be

if (db_affected_rows() == -1) {


lalit774’s picture

if i have run following code in a test file in drupal 6 root. i will get same result every time. db_affected_rows() return 1 result every time. if i have change node table name to non existing table like node_#$#$. Then db_affected_rows() also give me 1 result.

include_once './includes/';

$record = new stdClass();
$record->nid  = 1;
$record->title = 'test video123';
$record->status = 1;

// you can change node  table to node1 you can change any thing. you will get same result.

echo  db_query("UPDATE {node} SET title = '%s', status = '%d' WHERE nid = %d", $record->title, $record->status, $record->nid);

mdupont’s picture

You get 1 result when specifying a non-existing table because of #195812 . When there is an error during db_query() and that you have dblog enabled, there will be an error message written to the DB, and the "1" you see is because the write of the error message is successful. Silly design bug.

mdupont’s picture

Status:Active» Closed (cannot reproduce)

I don't see any bug here, referring to the mysqli documentation:

An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records where updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.

It says that -1 is returned only when there is an error in the query. Trying to UPDATE a row that doesn't exist is not a query error, and it returns that 0 rows were affected.

When I run db_query("UPDATE {node} SET status = 1 WHERE node = 1"); on a site that has no node 1, db_affected_rows() returns 0 as expected. If it wouldn't work that way a large set of core modules would have been severely broken.

lalit774’s picture

There is a bug in MySQL.

Please see below.

lalit774’s picture

Issue summary:View changes

missing code brace