The number of rows affected by an update operation in MYSQLI can be returned in two ways:
1. Returns the number of rows matched
2. Returns the number of rows affected
By default, the value returned by Mysqli_affected_rows is the number of rows affected, and if we need to return a matching number of rows, you can initialize the database connection using the Mysqli_real_connect function and add the flag parameter bits of the function:
Mysqli_client_found_rows return number of matched rows, not the number of affected rows
Normal format:
int mysqli_affected_rows (mysqli link)
OOP format:
Class Mysqli {
int Affected_rows
}
Look at the example:
Normal mode
The code is as follows |
Copy Code |
<?php $link = Mysqli_connect ("localhost", "My_user", "My_password", "World"); if (! $link) { printf ("Can" T connect to localhost. Error:%sn ", Mysqli_connect_error ()); Exit (); } /* Insert Rows/* Mysqli_query ($link, "CREATE TABLE Language select * from Countrylanguage"); printf ("Affected Rows (INSERT):%dn", Mysqli_affected_rows ($link)); Mysqli_query ($link, "ALTER TABLE Language ADD Status int default 0″); /* Update Rows/* Mysqli_query ($link, "UPDATE Language SET status=1 WHERE percentage > 50″);" printf ("Affected Rows (UPDATE):%dn", Mysqli_affected_rows ($link)); /* Delete rows/* Mysqli_query ($link, "DELETE from Language WHERE percentage < 50″);" printf ("Affected Rows (DELETE):%dn", Mysqli_affected_rows ($link)); /* Select all rows */ $result = Mysqli_query ($link, "Select CountryCode from Language"); printf ("Affected Rows (SELECT):%dn", Mysqli_affected_rows ($link)); Mysqli_free_result ($result); /* Delete Table Language * * Mysqli_query ($link, "DROP TABLE Language"); /* Close Connection * * Mysqli_close ($link); ?> |
OOP mode
The code is as follows |
Copy Code |
<?php $mysqli = new Mysqli ("localhost", "My_user", "My_password", "World"); /* Check Connection * * if (Mysqli_connect_errno ()) { printf ("Connect failed:%sn", Mysqli_connect_error ()); Exit (); } /* Insert Rows/* $mysqli->query ("CREATE TABLE Language select * from Countrylanguage"); printf ("Affected Rows (INSERT):%dn", $mysqli->affected_rows); $mysqli->query ("ALTER TABLE Language ADD Status int default 0″);" /* Update Rows/* $mysqli->query ("UPDATE Language SET status=1 WHERE percentage > 50″);" printf ("Affected Rows (UPDATE):%dn", $mysqli->affected_rows); /* Delete rows/* $mysqli->query ("DELETE from Language WHERE percentage < 50″);" printf ("Affected Rows (DELETE):%dn", $mysqli->affected_rows); /* Select all rows */ $result = $mysqli->query ("Select CountryCode from Language"); printf ("Affected Rows (SELECT):%dn", $mysqli->affected_rows); $result->close (); /* Delete Table Language * * $mysqli->query ("DROP TABLE Language"); /* Close Connection * * $mysqli->close (); ?> |