简体   繁体   中英

mysqli_stmt_affected_rows doesn't work for update prepared statement

I have this code

$SQL = "UPDATE cart SET a = ? WHERE b = ? AND c = ?";

            if($stmt2 = mysqli_prepare($conn, $SQL )){
                        mysqli_stmt_bind_param($stmt2, "iii", $a1, $b1, $c1);
                        mysqli_stmt_execute($stmt2);

                if(mysqli_affected_rows($conn)){
                    echo "updated";
                    mysqli_stmt_close($stmt2);
                }
                else
                    echo "nope";
            }

I was figuring out why above code never work. I have debug every way that I know.

Lastly I delete the if condition. Somehow, it is working. Tried to browse on the internet to figure out what is the cause but did not find any. Can somebody please explain to me why does it happen so? I am a newbie to mysqli. Really need help :) thanks!

You should be using

mysqli_stmt_affected_rows($stmt2);

mysqli_stmt::$affected_rows -- mysqli_stmt_affected_rows — Returns the total number of rows changed, deleted, or inserted by the last executed statement

Instead of

mysqli_affected_rows($conn);

which is for plain query executions.

Manual

Edit

I just noticed that your question title mentions the right function name, and your code uses the wrong one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM