I have a procedure with a mix of SELECT
commands and UPDATE
commands. Using PHP and PDO, for each query, I want to see either the results or the affected rows; however, this doesn't work for UPDATE
, INSERT
, or DELETE
queries (unless they are the only query in the procedure). I am able to retrieve the results from the SELECT
commands using $stmt->nextRowset();
however, I cannot retrieve the total number of records that have been updated, deleted, or inserted. How can I retrieve the affected rows?
Here is my PHP code:
$stmt = $connection->query($query);
do {
// updates the counter for each procedure query
$j++;
// gets the total rows from each rowset
$totalRows = $stmt->rowCount();
$data = getColumnData($connection, $stmt);
// checks if any data was returned
if (!$data) {
$rowText = $totalRows == 1 ? "row was" : "rows were";
$queryData .= $totalRows != 0 ? "<p><label>Procedure Query {$j}</label></p><p class='success'>A total of {$totalRows} {$rowText} affected!</p>" :"<p><label>Procedure Query {$j}</label></p><p><i class='fa fa-exclamation-triangle'></i> No results were returned! Check the query outside the procedure.</p>";
// displays the rows affected for other queries
} else {
$queryData .= "<p><label>Procedure Query {$j}</label></p><p class='success'>A total of {$totalRows} rows were returned!</p>{$data}";
}
} while ($stmt->nextRowset());
Here is a sample procedure that I quickly wrote up to demonstrate what the procedure looks like:
DELIMITER $$
CREATE PROCEDURE get_orders(IN vDate Date)
BEGIN
SELECT *
FROM `order`;
UPDATE `order`
SET `order_date` = vDate;
SELECT *
FROM `order`;
END$$
DELIMITER ;
CALL get_orders(Curdate() - 1);
This is the output but as you can see there is a missing result for the UPDATE
between queries one and two: