mysqli::$affected_rows/mysqli_affected_row() is returning 0 for me for some reason. This i my function:
function set_timezone_value($sUsername, $sTimeZone){
$_SESSION["timezone_value"] = $sTimeZone;
//get a connection to the LDAP db
$db_con = get_db_con(LDAP, TRUE);
$sQuery = "UPDATE user_info " .
"SET sTimeZone = '". $db_con->real_escape_string($sTimeZone) . "' " .
"WHERE uid = '". $db_con->real_escape_string($sUsername) ."'";
do_query($sQuery, $db_con);
var_dump($db_con->info);
$iAffectedRows = $db_con->affected_rows;
$db_con->close();
return $iAffectedRows;
}
do_query and get_db_con are simply wrappers for mysqli->query and new mysqli with additional code to send emails if there is an error.
That var_dump is printing string(40) "Rows matched: 1 Changed: 0 Warnings: 0"
which indicates that the row is being found but not changed. This is incorrect though since when I view the row in phpMyAdmin before and after the code is run, the row is actually updated to the new time zone, and the rest of the site functions with the correct timezone. Despite this, whether I use the procedural or the OO style, affected_rows and info indicate that no change to the row was made.
Is there something I'm messing up in my code?
The behavior you describe is the default. An UPDATE
statement with values matching the existing row results in zero rows affected. This is quite annoying as you do not know whether the record to update has been deleted in the meantime or there are simply no changes.
If you use PHP PDO and PHP 5.3+, there is an option to change that behavior:
$options = array('MYSQL_ATTR_FOUND_ROWS' => true);
Setting this flag returns the number of found (matched) rows, not the number of changed rows. See http://www.php.net/manual/en/ref.pdo-mysql.php and http://www.php.net/manual/en/pdostatement.rowcount.php#104930