Php updates mysql and obtains the method for changing the number of rows. mysql _ PHP Tutorial

Source: Internet
Author: User
Tags php sample code
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 :...

Related Article

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.