This article mainly introduces the returned values of mysqli_affected_rows for the number of rows in PHP. The example analyzes the usage of the common mode and oop mode, which has some reference value, for more information, see the examples in this article to analyze the returned values of the number of rows of mysqli_affected_rows in PHP. Share it with you for your reference. The specific analysis is as follows:
The number of rows affected by the update operation in MySQL can be returned in two forms:
1. the number of matched rows is returned.
2. the number of affected rows is returned.
By default, the value returned by mysqli_affected_rows is the number of affected rows. if we need to return the matched rows, we can use the mysqli_real_connect function to initialize the database connection and add the following to the flag parameter bit of the function:
MYSQLI_CLIENT_FOUND_ROWS return number of matched rows, not the number of affected rows
Common format:
The code is as follows:
Int mysqli_affected_rows (mysqli link)
Oop format:
The code is as follows:
Class mysqli {
Int affected_rows
}
Let's take a look at the example:
Normal modeThe code is as follows:
The code is as follows:
<? 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 modeThe code is as follows:
The code is as follows:
<? 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 ();
?>
I hope this article will help you with php programming.