mysqli_affected_rows() returns -1 but query works

2019-04-02 14:02发布

All my mysql functions seem to be having this problem. The simple insert statement works, but when I check the affected rows, it returns -1.

This works, a new row is inserted into the database with it, but debugger shows the mysqli_affected_rows as -1, and therefore the else block gets run:

$query = "INSERT INTO users (email, password) VALUES ('$email', '$password')";

mysqli_query($this->connection, $query) or die('Query failed in register.');


if (mysqli_affected_rows($this->connection)>0)  {
    //this doesnt get run, 
}
else {
   // this is run
}

Why would this happen?

Edit:

  • I'm using Netbeans, Xdebug, WAMP.
  • When I view the connection immediately after it is created, the affected rows is already set to -1.
  • After running a successful query, the connection still shows -1, before doing the affected rows check.
  • No error messages or error numbers are seen in the connection object.

1条回答
Evening l夕情丶
2楼-- · 2019-04-02 14:27

I had exactly the same problem. I've investigated the issue with Netbeans and Xdebug and it seems this is a bug in the MySQLi extension itself. An according bug report has been made. In the meantime you can instead use a more forgiving Expression, e.g.

if (mysqli_sqlstate($dbc) == 00000) {
//your code
}

to continue debugging your remaining code.

查看更多
登录 后发表回答