简体   繁体   English

Mysql存储过程-如果未找到数据,则返回表头

[英]Mysql Stored Procedure - Return Table headers if no Data found

We are using magento and PHP. 我们正在使用magento和PHP。 And have a stored Procedure which do some processing and its finally suppose to return multiple results set using select queries. 并具有一个存储过程,该存储过程进行一些处理,最后假设它会使用选择查询返回多个结果集。

Eg : Select * FROM table1;
     Select * FROM table2;
     Select * FROM table3;

The issue is that, not all tables might have data in it. 问题是,并非所有表都可能包含数据。 If there is no data in table stored procedure returns nothing. 如果表存储过程中没有数据,则不返回任何内容。 And in the PHP we are using a for loop to fetch data. 在PHP中,我们使用for循环来获取数据。

for($i= 0; $i<=3; $i++){
        $rowset = $sql->fetchAll(PDO::FETCH_ASSOC);
        if ($rowset) {
          switch ($i) {
            case 0:
              $spStatus = $rowset;
              break;
            case 1:
              $boqSections = $rowset;
              break;
            case 2:
              $boqEntries = $rowset;
              break;
            case 3:
              $boqItems = $rowset;
              break;
          }
        }
        $sql->nextRowset();
    }

So if there is no data found in table3. 因此,如果在表3中找不到数据。 Stored procedure will not return a 3rd rowset/resultset. 存储过程将不返回第三个行集/结果集。 So in the above loop a 3rd call to $sql->fetchAll(PDO::FETCH_ASSOC); 因此,在上面的循环中,第三个调用$sql->fetchAll(PDO::FETCH_ASSOC); will fail causing a General error. 将失败并导致常规错误。

Are there any work around to solve this issue? 有没有解决此问题的方法?

Add below condition for rowCount 为rowCount添加以下条件

   for($i= 0; $i<=3; $i++){
    $rowset = $sql->fetchAll(PDO::FETCH_ASSOC); 
    if ($rowset && $sql->rowCount() >0) {
       switch ($i) {
        case 0:
          $spStatus = $rowset;
          break;
        case 1:
          $boqSections = $rowset;
          break;
        case 2:
          $boqEntries = $rowset;
          break;
        case 3:
          $boqItems = $rowset;
          break;
      }
     } else {
       $spStatus = [];
       $boqSections = [];
       $boqEntries = [];
       $boqItems = [];
     }

     $sql->nextRowset();
   }

Hope This will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM