whatever
whatever

Reputation: 342

affected_rows & updating mysql from php

When I try to update mysql from php with the same value as the existing one in the data then affected_rows returns zero. Which means the database operation went successful anyway. So I want to return the successful message, irrespective of the same value as before. but based on the affected_rows value I can't say anything. So in this situation how should i check that the operation was successful anyway? The below will be an example case....

$query='update chapter set ChapterName=? where ChapterId=?';
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt-> bind_param('si', $chaptername, $chapterid);
$stmt->execute();
if($stmt->affected_rows>0)
{return true;}

EDIT I have removed the typos.. that was my mistake

Upvotes: 0

Views: 172

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34426

You have to attach affected_rows() to the connection, not the statement:

if($mysqli->affected_rows >= 1) {
    // success
} else {
    // failure
}

Upvotes: 0

Uueerdo
Uueerdo

Reputation: 15961

Depending on your connection method, there is usually a connection option to return matched rows instead of changed rows.

If you're using mysqli this might be the answer: https://stackoverflow.com/a/5289535/4104224

This one looks a little more thorough and explicit though: https://stackoverflow.com/a/2925972/4104224

Upvotes: 1

Related Questions