PHP-MSSQL的事务处理问题

时间:2017-12-18 10:55:50

标签: php sql-server

我有一个场景,我的多个Select / Update / Select语句最终在我的基于网络的应用中显示结果集。我不知道如何处理这种情况。下面是相同的代码。有人可以建议我如何实现这个目标?目前,它在某处失败并且在网页上没有显示任何内容。甚至没有错误。

   <?php 

                    //$ConnectionInfo=dbConnectSolaProdDBServer();

                    if ($ConnectionInfo == false) {
                        die( print_r( sqlsrv_errors(), true));
                        //die("Something didn't go too smoothly! Try refreshing?");
                    }

                    $query1 = $SOLADBCashComponentCheck;

                    $query2 = "UPDATE #tmpInterim
                                SET BMPerformance = (BenchMarkValueOpenBase - BenchMarkValueCloseBase) / BenchMarkValueCloseBase
                                WHERE BenchMarkValueCloseBase IS NOT NULL;";

                    $query3 = "UPDATE #tmpInterim
                                SET MTM = SSMTM * (1 + BMPerformance)
                                       ,NAVPerShare = SSNAV * (1 + BMPerformance) - ((( CAST(DATEDIFF(DAY,SSNAVDate,NAVDate) AS DECIMAL(38,34))) / CAST(360 AS DECIMAL(25,13))) * ((ISNULL(Spread,0.00000000000000) + TER) / 10000) * SSNAV * (1 + BMPerformance));";

                    $query4 = "/* Calculate total shares in issue */
                                UPDATE #tmpInterim
                                SET TotalFundNAV = NAVPerShare * TotalSharesInIssue;";

                    $query5 = "select ETFID, FundName, Currency, OpenDate, NAVDate, SSNAVDate, NAVPerShare, TotalSharesInIssue, TotalFundNAV, MTM, 
                                    ((SSNAV * SSSO) - SSMTM)/SSAUM as 'SS Diff(%)', ((NAVPerShare * TotalSharesInIssue) - MTM)/TotalFundNAV as 'MIMS Diff(%)' 
                                from 
                                    #tmpInterim  
                                where 
                                    ((SSNAV * SSSO) - SSMTM)/SSAUM > 0.000001 or  ((NAVPerShare * TotalSharesInIssue) - MTM)/TotalFundNAV > 0.000001;";


                    /* Begin the transaction. */
                    if ( sqlsrv_begin_transaction( $ConnectionInfo ) === false ) {
                         die( print_r( sqlsrv_errors(), true ));
                    }

                    if (($stmt1 = sqlsrv_query($ConnectionInfo,$query1)) == null) {
                        die(print_r(sqlsrv_errors(), true));
                    }   
                    if ($stmt2 = sqlsrv_query( $ConnectionInfo, $query2)) {
                        die( print_r( sqlsrv_errors(), true ));
                    }

                    if ($stmt3 = sqlsrv_query( $ConnectionInfo, $query3)) {
                        die( print_r( sqlsrv_errors(), true ));
                    }

                    if ($stmt4 = sqlsrv_query( $ConnectionInfo, $query4)) {
                        die( print_r( sqlsrv_errors(), true ));
                    }

                    if( $stmt1 && $stmt2 && $stmt3 && $stmt4 ) {
                        sqlsrv_commit( $ConnectionInfo );
                        echo "Transaction committed.<br />";

                        if (($result = sqlsrv_query($ConnectionInfo,$query5)) == null) {
                        echo "Died: ";
                            die(print_r(sqlsrv_errors(), true));
                        //die("Something didn't go too smoothly! Try refreshing?");
                        }
                        else
                        {
                            $numRows = sqlsrv_num_rows($result);
                            do {                            
                                while($row = sqlsrv_fetch_array($result))
                                {

                                    echo "<tr class='odd gradeX'>
                                                <td>$row[0]</td>
                                                <td>$row[1]</td>
                                                <td>$row[2]</td>
                                                <td>$row[3]</td>
                                                <td>$row[4]</td>
                                                <td>$row[5]</td>
                                                <td>$row[6]</td>
                                                <td>$row[7]</td>
                                                <td>$row[8]</td>
                                                <td>$row[9]</td>
                                                <td>$row[10]</td>
                                            </tr>";
                                }
                            } while (sqlsrv_next_result($result));
                                echo "</tbody>";
                        }
                    } else {
                        sqlsrv_rollback( $ConnectionInfo );
                         echo "Transaction rolled back.<br />";
                    }

                    // Close the connection to MSSQL
                    sqlsrv_close($ConnectionInfo);

                    ?>

1 个答案:

答案 0 :(得分:0)

管理以解决此问题。

$query="BEGIN TRAN;
QUERY1: SELECT INTO #TEMP TABLE;
QUERY2: UPDATE #TEMP TABLE;
QUERY3: UPDATE #TEMP TABLE;
QUERY4: UPDATE #TEMP TABLE;
QUERY5: SELECT COLUMN1, COLUMN2,...COLUMN11 from #TEMP TABLE.
COMMIT TRAN;";

上面的代码全部用PHP执行,如下所示:

                    if (($result = sqlsrv_query($ConnectionInfo,$query)) == null) {
                        echo "Died: ";
                            die(print_r(sqlsrv_errors(), true));
                        //die("Something didn't go too smoothly! Try refreshing?");
                        }
                        else
                        {
                            $numRows = sqlsrv_num_rows($result);
                            do {                            
                                while($row = sqlsrv_fetch_array($result))
                                {

                                    echo "<tr class='odd gradeX'>
                                                <td>$row[0]</td>
                                                <td>$row[1]</td>
                                                <td>$row[2]</td>
                                                <td>$row[3]</td>
                                                <td>$row[4]</td>
                                                <td>$row[5]</td>
                                                <td>$row[6]</td>
                                                <td>$row[7]</td>
                                                <td>$row[8]</td>
                                                <td>$row[9]</td>
                                                <td>$row[10]</td>
                                                <td>$row[11]</td>
                                            </tr>";
                                }
                            } while (sqlsrv_next_result($result));
                                echo "</tbody>";
                        }
                    // Close the connection to MSSQL
                    sqlsrv_close($ConnectionInfo);

                    ?>

结果集在网页上呈现正常。

感谢大家的投入。