In PHP, mysqli_affected_rows analyzes the returned values of the number of rows. in mysqlinumrows_PHP tutorial

Source: Internet
Author: User
In PHP, mysqli_affected_rows analyzes the returned values of the number of rows, and mysqlinumrows. In PHP, the returned values of mysqli_affected_rows are analyzed. In this example, the returned values of mysqli_affected_rows are analyzed in PHP. Share it with you for your reference. In PHP, mysqli_affected_rows analyzes the returned values of the number of rows, and mysqlinumrows

This article analyzes the returned values of the number of rows in PHP mysqli_affected_rows. Share it with you for your reference. The specific analysis is as follows:

The number of rows affected by the update operation in MySQL can be returned in two forms:

1. the number of matched rows is returned.
2. the number of affected rows is returned.

By default, the value returned by mysqli_affected_rows is the number of affected rows. if we need to return the matched rows, we can use the mysqli_real_connect function to initialize the database connection and add the following to the flag parameter bit of the function:

MYSQLI_CLIENT_FOUND_ROWS return number of matched rows, not the number of affected rows

Common format:

The code is as follows:

Int mysqli_affected_rows (mysqli link)

Oop format:

The code is as follows:

Class mysqli {
Int affected_rows
}


Let's take a look at the example:

Normal modeThe code is as follows:

The code is as follows:

<? Php
$ Link = mysqli_connect ("localhost", "my_user", "my_password", "world ");

If (! $ Link ){
Printf ("Can't connect to localhost. Error: % sn", mysqli_connect_error ());
Exit ();
}

/* Insert rows */
Mysqli_query ($ link, "create table Language SELECT * from CountryLanguage ");
Printf ("Affected rows (INSERT): % dn", mysqli_affected_rows ($ link ));

Mysqli_query ($ link, "alter table Language ADD Status int default 0 ″);

/* Update rows */
Mysqli_query ($ link, "UPDATE Language SET Status = 1 WHERE Percentage> 50 ″);
Printf ("Affected rows (UPDATE): % dn", mysqli_affected_rows ($ link ));

/* Delete rows */
Mysqli_query ($ link, "delete from Language WHERE Percentage <50 ″);
Printf ("Affected rows (DELETE): % dn", mysqli_affected_rows ($ link ));

/* Select all rows */
$ Result = mysqli_query ($ link, "SELECT CountryCode FROM Language ");
Printf ("Affected rows (SELECT): % dn", mysqli_affected_rows ($ link ));

Mysqli_free_result ($ result );

/* Delete table Language */
Mysqli_query ($ link, "drop table Language ");

/* Close connection */
Mysqli_close ($ link );
?>


Oop modeThe code is as follows:

The code is as follows:

<? Php
$ Mysqli = new mysqli ("localhost", "my_user", "my_password", "world ");

/* Check connection */
If (mysqli_connect_errno ()){
Printf ("Connect failed: % sn", mysqli_connect_error ());
Exit ();
}

/* Insert rows */
$ Mysqli-> query ("create table Language SELECT * from CountryLanguage ");
Printf ("Affected rows (INSERT): % dn", $ mysqli-> affected_rows );

$ Mysqli-> query ("alter table Language ADD Status int default 0 ″);

/* Update rows */
$ Mysqli-> query ("UPDATE Language SET Status = 1 WHERE Percentage> 50 ″);
Printf ("Affected rows (UPDATE): % dn", $ mysqli-> affected_rows );

/* Delete rows */
$ Mysqli-> query ("delete from Language WHERE Percentage <50 ″);
Printf ("Affected rows (DELETE): % dn", $ mysqli-> affected_rows );

/* Select all rows */
$ Result = $ mysqli-> query ("SELECT CountryCode FROM Language ");
Printf ("Affected rows (SELECT): % dn", $ mysqli-> affected_rows );

$ Result-> close ();

/* Delete table Language */
$ Mysqli-> query ("drop table Language ");

/* Close connection */
$ Mysqli-> close ();
?>

I hope this article will help you with php programming.

Examples in this article analyze the returned values of the number of rows affected by mysqli_affected_rows in PHP. Share it with you for your reference. Details...

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.