Database Reference
In-Depth Information
To tell MySQL whether to return rows-changed or rows-matched counts, specify
mysql_client_found_rows
in the options part of the data source name (DSN) argument
of the
connect()
call when you connect to the MySQL server. Set the option to 0 for
rows-changed counts and 1 for rows-matched counts. Here's an example:
my
$conn_attrs
=
{
PrintError
=>
0
,
RaiseError
=>
1
,
AutoCommit
=>
1
};
my
$dsn
=
"DBI:mysql:cookbook:localhost;mysql_client_found_rows=1"
;
my
$dbh
=
DBI
->
connect
(
$dsn
,
"cbuser"
,
"cbpass"
,
$conn_attrs
);
mysql_client_found_rows
changes the row-reporting behavior for the duration of the
session.
Although the default behavior for MySQL itself is to return rows-changed counts, cur‐
rent versions of the Perl DBI driver for MySQL automatically request rows-matched
counts unless you specify otherwise. For applications that depend on a particular be‐
havior, it's best to explicitly set the
mysql_client_found_rows
option in the DSN to the
appropriate value.
Ruby
In Ruby DBI scripts, the
do
method returns the row count for statements that modify
rows:
count
=
dbh
.
do
(
stmt
)
puts
"Number of rows affected:
#{
count
}
"
If you use
execute
to execute a statement, use the statement handle
rows
method to get
the count afterward:
sth
=
dbh
.
execute
(
stmt
)
puts
"Number of rows affected:
#{
sth
.
rows
}
"
The Ruby DBI driver for MySQL returns rows-changed counts by default, but the driver
supports a
mysql_client_found_rows
option that enables you to control whether the
server returns rows-changed or rows-matched counts. Its use is analogous to Perl DBI.
For example, to request rows-matched counts, do this:
dsn
=
"DBI:Mysql:database=cookbook;host=localhost;mysql_client_found_rows=1"
dbh
=
DBI
.
connect
(
dsn
,
"cbuser"
,
"cbpass"
)
PHP
In PDO, the database handle
exec()
method returns the rows-affected count:
$count
=
$dbh
->
exec
(
$stmt
);
printf
(
"Number of rows updated: %d
\n
"
,
$count
);
If you use
prepare()
plus
execute()
instead, the rows-affected count is available from
the statement handle
rowCount()
method: