php更新mysql后获取改变行数的方法_PHP

WBOY
Release: 2016-06-01 11:06:35
Original
814 people have browsed it

本文实例讲述了php更新mysql后获取改变行数的方法。分享给大家供大家参考。具体分析如下:

一个php更新mysql后获取改变的行数,在php中提供mysql函数来获取最后执行查询所影响的记录数:mysql_affected_rows(), 返回最近一次与 连接句柄 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数.FOUND_ROWS() : select ROW_COUNT():update delete insert.

下面就是文章的主要内容描述,代码如下:

代码如下:

found_rows():select
row_count(): update delete insert

注:需要配合相应的操作一起使用,否则返回的值只是1和-1(都是不正确的值)

php示例代码如下:

代码如下:

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(返回删除的行数)


php更新mysql后获取影响的行数发生异常解决,代码如下:

代码如下:

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 }

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template