获取 PHP 中存储过程的返回值,不带任何参数

问题描述 投票:0回答:1

我有点困惑如何使用 PHP 获取 SP SQL Server 的返回值。

它根本不显示任何内容,但如果我在 SSMS 中运行

EXEC SP_NAME
,它看起来很好。

有人可以教我并帮助我怎么做吗?不知道我的代码有没有问题?

如你所见,当我在 SSMS 中运行它时,一切都很好:

enter image description here

但是当我在 PHP 中运行 SP Execution 脚本时,没有任何显示:

enter image description here

如果我使用

var_dump()
print_r()
进行调试,我可以查看是否有资源:

enter image description here

这是我的 PHP 脚本:

$conn = sqlsrv_connect( $serverName, $connectionInfo);
if ( $conn === false ) {
    echo "Could not connect to server. Lagi Maintenance Sabar\n";
    // die( print_r( sqlsrv_errors(), true));
}

$get_data   = "exec SP_MCM_Monitoring";

$stmt       = sqlsrv_query($conn,$get_data);
        
if ($stmt === false) {
    die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    print_r($row, true);
}

谁能告诉我,通过PHP执行SP时如何获取SP的返回值?

php arrays sql-server stored-procedures sqlsrv
1个回答
0
投票
<?php
    $serverName = "your_server_name";  // Replace with your actual server name
    $connectionInfo = array("Database"=>"your_database", "UID"=>"your_username", "PWD"=>"your_password");
    
    // Establish the connection
    $conn = sqlsrv_connect($serverName, $connectionInfo);
    
    if ($conn === false) {
        echo "Could not connect to server. Maintenance ongoing, please wait...\n";
        die(print_r(sqlsrv_errors(), true));  // Output detailed connection errors
    }
    
    // Define and execute the stored procedure
    $get_data = "EXEC SP_MCM_Monitoring";  // Call the stored procedure
    
    $stmt = sqlsrv_query($conn, $get_data);
    
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));  // Handle query execution errors
    }
    
    // Fetch and display the data
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        print_r($row);  // Display the row (removed the 'true' argument so it outputs)
    }
    
    // Free the statement and close the connection
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.