How can we get the total number of rows affected by MySQL query?

MySQLMySQLi Database

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.

Example

mysql> Delimiter //
mysql> CREATE PROCEDURE `query`.`row_cnt` (IN command VarChar(60000))
    -> BEGIN
    ->    SET @query = command;
    ->    PREPARE stmt FROM @query;
    ->    EXECUTE stmt;
    ->    SELECT ROW_COUNT() AS 'Affected rows';
    -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> Delimiter ;
mysql> Create table Testing123(First Varchar(20), Second Varchar(20));
Query OK, 0 rows affected (0.48 sec)

mysql> CALL row_cnt("INSERT INTO testing123(First,Second) Values('Testing First','Testing Second');");
+---------------+
| Affected rows |
+---------------+
|             1 |
+---------------+
1 row in set (0.10 sec)

Query OK, 0 rows affected (0.11 sec)

The above result set shows that I row is affected after inserting the data into the ‘testing123’ table.

raja
Published on 22-Feb-2018 09:53:03
Advertisements