dqyp50298 2019-01-30 23:10
浏览 263

PHP PDO:如何显示受影响的UPDATE行以及带有过程的SELECT结果?

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: enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧