2

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.
13
  • What about if (mysqli_affected_rows($this->connection, $query)>0) May 29, 2014 at 23:52
  • Didn't work, still -1
    – Andrew
    May 29, 2014 at 23:56
  • Can you paste your query, mysqli_affected_rows returns -1 on query error
    – Ian
    May 30, 2014 at 0:24
  • Why do you think it returns -1?
    – zerkms
    May 30, 2014 at 0:36
  • 1
    i think there is an error with your debugger stackoverflow.com/questions/23848434/… May 30, 2014 at 3:49

1 Answer 1

3

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.

2
  • Decent idea checking for an error at least if not the affected rows... However, that bug report indicates $dbc->stat causes the affected_rows to become -1, but I am not using the stat feature.
    – Andrew
    Jun 3, 2014 at 0:55
  • Thank you! The bug still exists at this time on Linux Mint 19 with MariaDB Ver 15.1 Distrib 10.1.38-MariaDB and php 7.2.17.
    – Moini
    May 21, 2019 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.