Php updates mysql and obtains the changed number of rows _ PHP Tutorial

Source: Internet
Author: User
Php updates mysql and obtains the number of changed rows. 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 changed rows after the last php update mysql with the connection handle. provides the mysql function in php 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:

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)

Example:

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 error occurred while retrieving affected rows after updating mysql in php.

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;
}

Callback (), returns the last connection handle close...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.