Home > Database > Mysql Tutorial > body text

How to Get the Number of Affected Rows in MySQL Queries Run from Bash?

DDD
Release: 2024-10-29 20:13:30
Original
689 people have browsed it

How to Get the Number of Affected Rows in MySQL Queries Run from Bash?

How to Retrieve Number of Rows Affected in MySQL Queries Executed from Bash

When executing MySQL queries or commands from bash, it's often useful to know how many rows are affected by the operation. This information can be valuable for debugging, logging, or tracking progress.

Using the following syntax, you can execute MySQL queries from bash:

<code class="bash">mysql -u[user] -p[pass] -e "[mysql commands]"</code>
Copy after login

or

<code class="bash">mysql -u[user] -p[pass] `<<`QUERY_INPUT

[mysql commands]

QUERY_INPUT</code>
Copy after login

However, these methods do not return the number of affected rows directly.

To capture this information, you can add SELECT ROW_COUNT(); as the last statement in your batch. The output will then include the number of rows affected. You can parse this output to extract the desired data.

For example, to count the number of rows updated by a query, you could run:

<code class="bash">mysql -u[user] -p[pass] -e "[update query];SELECT ROW_COUNT();"</code>
Copy after login

The output of this command would include the number of rows updated, which you can then parse as needed.

The above is the detailed content of How to Get the Number of Affected Rows in MySQL Queries Run from Bash?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template