php調用sql server的存儲過程(傳參版)

    $serverName="localhost";
    $connectionInfo = array("UID"=>"sa", "PWD"=>"123456", "Database"=>"master", "CharacterSet"=>"utf-8");
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    if($conn === false){
        die(print_r(sqlsrv_errors(), true));
    }  
     $startMonth = '201901';
    $endMonth = '201902';
        $params = array( 
                     array($startMonth, SQLSRV_PARAM_IN),
                     array($endMonth, SQLSRV_PARAM_IN)
                   );   
    $sp = "{call p_hslxgb_xxzx(?,?)}";
    $stmt = sqlsrv_query($conn, $sp, $params);
    if($stmt === false){
        die(print_r(sqlsrv_errors(), true));
    }

    $arr = array();
    $n=0;
    
    sqlsrv_next_result($stmt);
    sqlsrv_next_result($stmt);


  while(  $row  =  sqlsrv_fetch_array (  $stmt ,  SQLSRV_FETCH_ASSOC ) ) {
        $arr[$n] = array(
                           'company'=>$row [ 'company' ],
                           'receivable_fee'=>$row [ 'receivable_fee' ],
                           'receipts_fee'=>$row [ 'receipts_fee' ] ,
                           'mtc_rate'=>$row [ 'mtc_rate' ] ,
                           'total_rate'=>$row [ 'total_rate' ] ,
                           'com_rate'=>$row [ 'com_rate' ] ,
                           'bank_rate'=>$row [ 'bank_rate' ] 
          );
          $n++;
  }
    echo json_encode($arr); 
    sqlsrv_free_stmt (  $stmt );
    sqlsrv_close( $conn);

確定存儲過程寫的沒問題,php調用後,就是獲取不到返回結果。

    sqlsrv_next_result($stmt);
    sqlsrv_next_result($stmt);

這兩句是關鍵,因爲

After "$stmt = sqlsrv_query()", $stmt will contain the number of rows affected by UPDATE statement, and no rows, so sqlsrv_fetch_array() returns nothing.

Then call sqlsrv_next_result() , and $stmt will contain rows returned from select statement.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章