Mysqli_affected_rows action row number return value in PHP

Source: Internet
Author: User

The number of rows affected by an update operation in MYSQLI can be returned in two ways:

1. Returns the number of rows matched

2. Returns the number of rows affected

By default, the value returned by Mysqli_affected_rows is the number of rows affected, and if we need to return a matching number of rows, you can initialize the database connection using the Mysqli_real_connect function and add the flag parameter bits of the function:


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


Normal format:
int mysqli_affected_rows (mysqli link)
OOP format:
Class Mysqli {
int Affected_rows
}
Look at the example:
Normal mode

The code is as follows Copy Code

<?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 mode

The code is as follows Copy Code

<?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 ();
?>

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.