Andrew Larsen
Andrew Larsen

Reputation: 1267

mysqli->query returns true on update, but no rows affected

I am experiencing some issues when trying to update rows in mysql.

I've tried to print the query and run it in my sql client, and it updates the rows as expected.

This line:

$this->mysqli->query($query);

Returns bool (true) as expected, and is correct according to the php documentation (http://php.net/manual/en/mysqli.query.php).

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

public function query($query, $parameters = array()) {
    if ((is_array($parameters) && count($parameters) > 0) || (is_string($parameters) && !empty($parameters))) {
        $this->lastPrepared = true;

        if ($this->prepare($query) === false)
            return false;

        $this->bind((is_array($parameters) ? $parameters : array($parameters)));
        if ($this->execute() === false)
            return false;
    } else {
        $this->lastPrepared = false;

        if (!$this->stmt = $this->mysqli->query($query)) {
            debug('Query failed: (' . $this->mysqli->errno . ') ' . $this->mysqli->error);
            return false;
        }
    }

    return $this->stmt;
}

I have searched around, and found questions similar to my question, but non of the answers have helped me found a solution.

EDIT: Okay, so I can see now that $this->mysqli->affected_rows; returns -1, which means that there is an error. However, mysqli->errno and mysqli->error are empty.

The query I am trying to run is:

UPDATE payments_scheduled SET active = 0 WHERE id=1 OR id=2

Columns:

id - type: int - length: 10 - extra: unsigned, pk

active - type: tinyint - length: 1 - extra: unsigned

Upvotes: 1

Views: 2831

Answers (2)

Andrew Larsen
Andrew Larsen

Reputation: 1267

Okay, so I found the problem.

I was starting a transaction within a method, and I never comitted.

My apologies for wasting your time, you could not have figured out the problem by looking at the code I provided. I was sure the issue was within that query method (but I was wrong).

I'll vote up each and everyone of you that tried to help out.

Upvotes: 1

Ilion
Ilion

Reputation: 6882

Updating 0 rows does not count as a failure, it simply means there were no rows to update based on the selection criteria. In many cases this would be expected behaviour and thus returns true.

Upvotes: 3

Related Questions