简体   繁体   中英

Mysql Stored Procedure - Return Table headers if no Data found

We are using magento and 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.

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. Stored procedure will not return a 3rd rowset/resultset. So in the above loop a 3rd call to $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

   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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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