Home  >  Article  >  Database  >  How to Capture Rows Affected by MySQL Queries from Bash?

How to Capture Rows Affected by MySQL Queries from Bash?

Susan Sarandon
Susan SarandonOriginal
2024-10-31 00:52:02244browse

How to Capture Rows Affected by MySQL Queries from Bash?

Capturing Rows Affected by MySQL Queries from bash

When executing MySQL queries or commands from bash using the commands:

mysql -u[user] -p[pass] -e "[mysql commands]"

or

mysql -u[user] -p[pass] `<<`QUERY_INPUT

[mysql commands]

QUERY_INPUT

you may encounter the need to determine the number of rows affected by the executed query.

To retrieve this information, you can't simply capture the output of the query as a variable, as doing so won't return the row count. Instead, consider adding the following statement to the end of your batch of MySQL commands:

SELECT ROW_COUNT();

This statement will return the number of rows affected by the preceding queries. By parsing the output of the modified batch, you can then retrieve the desired row count.

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

Statement:
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