Home > Database > Mysql Tutorial > body text

How to Capture the Number of Rows Affected by a MySQL Query from Bash?

Barbara Streisand
Release: 2024-11-02 16:06:29
Original
757 people have browsed it

How to Capture the Number of Rows Affected by a MySQL Query from Bash?

Capture Number of Rows Affected During MySQL Query Execution from Bash

When executing MySQL queries through the bash command line, you can retrieve the number of rows affected by the query using a specific technique.

To obtain this information, you can incorporate the SELECT ROW_COUNT(); statement as the concluding part of your SQL command block. By doing this, you enable the query to output the number of affected rows.

To capture this output in bash, you can use backticks () or the heredoc ( << QUERY_INPUT `) syntax. However, you'll need to parse the output to isolate the row count value.

For example, to execute the UPDATE query and obtain the affected row count, you would use:

<code class="bash">variable=$(mysql -u[user] -p[pass] -e "UPDATE table_name SET column_name = 'new_value' WHERE condition; SELECT ROW_COUNT();")</code>
Copy after login

Once you have the output stored in the $variable, you can retrieve the row count using appropriate string manipulation techniques, such as grep or sed.

The above is the detailed content of How to Capture the Number of Rows Affected by a MySQL Query 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!