Execute Success, but num_rows returns 0 [PHP-MySQL]

The problem I just got is

$update_stmt->execute()OK, and the data in the database is already updated

but $update_resultrow = $update_stmt->num_rows; returns 0?

I tried to copy the MySQL command to run in the query, and it also worked well, for example:

UPDATE ACCOUNT_EMPLOYEE SET NAME = 'cccccc' WHERE ID = 1

The problem code is here:

$update_sql = "UPDATE ACCOUNT_EMPLOYEE SET NAME = ? WHERE ID = ?";
if ($update_stmt = $conn -> prepare($update_sql)) {
    if ($update_stmt->bind_param("si",
        $newname,
        $acc_id
    )
    ) {
        if ($update_stmt->execute()) {
            // must declare here to be able to get num_rows
            $update_stmt->store_result();
            $update_resultrow = $update_stmt->num_rows;
            if ($update_resultrow == 0) {
                echo $error_forgot_noresult . '????' . $acc_id ;
                $update_stmt->close();
                $conn->close();
                exit();
            }
        }
    }
}
+3
source share
2 answers

Yes, Fred -ii-, I never noticed that he has → affected_rows. please write as an answer and I will mark it here

As requested by OP.

Seeing that the goal here is to check if the request is really successful, you need to use affected_rows.

According to the manual http://php.net/manual/en/mysqli.affected-rows.php

printf ( " (UPDATE):% d\n", $mysqli- > affected_rows);

  • -

int $mysqli- > affected_rows;


Sidenote:

$update_resultrow = $update_stmt->num_rows;

, "return 0".

+5

, , , :

$update_sql = "UPDATE ACCOUNT_EMPLOYEE SET NAME = ? WHERE ID = ?";
if ($update_stmt = $conn -> prepare($update_sql)) {
    if ($update_stmt->bind_param("si",
        $newname,
        $acc_id
    )
    ) {
        if ($update_stmt->execute()) {
            // must declare here to be able to get num_rows
            $update_stmt->store_result();
            $update_resultrow = $update_stmt->affected_rows;
            if ($update_resultrow == 0) {
                echo $error_forgot_noresult . '????' . $acc_id ;
                $update_stmt->close();
                $conn->close();
                exit();
            }
        }
    }
}
0

All Articles