This article describes the method of changing the number of rows after php updates mysql. Share it for your reference. The specific analysis is as follows:
After a php updates mysql, gets the changed number of rows. Provides the mysql function in php to get the number of records affected by the last query execution: mysql_affected_rows(), and returns the last INSERT, UPDATE or DELETE query associated with the connection handle. FOUND_ROWS(): select ROW_COUNT():update delete insert.
The following is the main content description of the article, the code is as follows:
row_count(): update delete insert
Note: It needs to be used with the corresponding operation, otherwise the returned values are only 1 and -1 (both incorrect values)
The php sample code is as follows:
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','role1' union all
select '2','role2' union all
select '3','role3';
select row_count(); -- Output 3 (returns the number of newly added records), [Note: If insert into...values is used, only 1 will be returned]
select * from `MyTestTable`;select found_rows(); -- Output 3 (returns the number of selected rows)
update `MyTestTable` set `Name`='people';select row_count(); -- Output 3 (returns the modified number of rows)
delete from `MyTestTable`;select row_count(); -- Output 3 (returns the number of deleted rows)
After php updates mysql, an exception occurs to get the affected number of rows and solve the problem. The code is as follows:
$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 be helpful to everyone's PHP programming.