Php updates mysql and obtains the method for changing the number of mysql rows. Php updates mysql to obtain the number of rows changed. This article describes how to obtain the number of rows changed after php updates mysql. Share it with you for your reference. The specific analysis is as follows: php updates mysql and obtains the method for changing the number of rows. the number of mysql rows
This example describes how to obtain the number of changed rows after php updates mysql. Share it with you for your reference. The specific analysis is as follows:
After updating mysql, a php instance obtains the number of changed rows. in php, the mysql function is provided to obtain the number of records affected by the last query: mysql_affected_rows (), returns the number of rows affected by the last INSERT, UPDATE, or DELETE query associated with the connection handle. FOUND_ROWS (): select ROW_COUNT (): update delete insert.
The following describes the main content of the article. the code is as follows:
The code is as follows:
Found_rows (): select
Row_count (): update delete insert
Note: it must be used together with the corresponding operation. Otherwise, the returned values are only 1 and-1 (both incorrect values)
The php sample code is as follows:
The code is as follows:
Drop database if exists 'mytest ';
Create database 'mytest ';
Use 'mytest ';
Drop table if exists 'mytesttable ';
Create table 'mytesttable' ('id' int, 'name' varchar (10 ));
Insert into 'mytesttable' ('id', 'name ')
Select '1', 'role 1' union all
Select '2', 'role 2' union all
Select '3', 'role 3 ';
Select row_count (); -- output 3 (returns the number of newly added Records). [note: If insert into... values is used, only 1 is returned.]
Select * from 'mytesttable'; select found_rows (); -- output 3 (return the number of selected rows)
Update 'mytesttable' set 'name' = 'people'; select row_count (); -- output 3 (returns the number of modified rows)
Delete from 'mytesttable'; select row_count (); -- output 3 (return the number of deleted rows)
An exception occurred when php updated mysql to obtain the affected number of rows. the code is as follows:
The code is as follows:
Function mysql_modified_rows (){
$ Info_str = mysql_info ();
$ A_rows = mysql_affected_rows ();
Ereg ("Rows matched: ([0-9] *)", $ info_str, $ r_matched );
Return ($ a_rows <1 )? ($ R_matched [1]? $ R_matched [1]: 0): $ a_rows;
}
I hope this article will help you with php programming.
In this example, php updates mysql and obtains the method to change the number of rows. Share it with you for your reference. The specific analysis is as follows :...