Get the number of changed rows after a php updates mysql. Provide a mysql function in php to get the number of records affected by the last executed query: mysql_affected_rows(), which returns the most recent INSERT, UPDATE or DELETE query associated with the connection handle. Number of record rows affected. FOUND_ROWS() : select ROW_COUNT() : update delete insert.
The following is the main content description of the article.
The code is as follows
代码如下 |
复制代码 |
found_rows() : select
row_count() : update delete insert
|
|
Copy code
|
found_rows() : select
row_count() : update delete insert
代码如下 |
复制代码 |
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','role1' union all
select '2','role2' union all
select '3','role3';
select row_count(); -- 输出3(返回新添加的记录数),[注:如果使用insert into...values只返回1]
select * from `MyTestTable`;select found_rows(); -- 输出3(返回选择的行数)
update `MyTestTable` set `Name`='people';select row_count(); -- 输出3(返回修改的行数)
delete from `MyTestTable`;select row_count(); -- 输出3(返回删除的行数)
|
代码如下 |
复制代码 |
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;
}
|
Note: It needs to be used in conjunction with the corresponding operation, otherwise the returned values are only 1 and -1 (both incorrect values) |
Example:
The code is as follows
|
Copy code
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','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 is 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 number of modified rows)
Solving the problem of getting the number of affected rows after php updates mysql
The code is as follows
|
Copy code
|
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;
}
http://www.bkjia.com/PHPjc/630723.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630723.htmlTechArticleGet the number of changed rows after updating mysql in php, and provide the mysql function in php to get the impact of the last executed query Number of records: mysql_affected_rows(), returns the latest connection handle...
|
|
|