List data from Procedure sqlsrv PHP

Asked

Viewed 65 times

2

Good morning,

someone with experience in PHP and microsoft sqlsrv drive ?

I found it difficult to display data from a Procedure in SQL SERVER with the "Microsoft sqlsrv driver".

Something strange is happening that does not generate any return.

What may be speaking or wrong in the code please.

$serverName = "SRVSQLDEALER\XXX"; 

$connectionInfo = array( "Database"=>"DB", "UID"=>"USER", "PWD"=>"KEY");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

if ( $conn ){  

$var1 = '3610';
$var2 = '2020-10-29';

$procedure_params = array(

array(&$myparams['CodigoEmpresa'], SQLSRV_PARAM_OUT),
array(&$myparams['DataReferencia'], SQLSRV_PARAM_OUT)

);

$sql = "EXECUTE dbRedeMineira.dbo.whCRLDocumentosReceber @CodigoEmpresa = ?, @DataReferencia = ? " ; 

$stmt = sqlsrv_query($conn,$sql,array($var1, $var2));

echo $conn."<br />";
echo $sql."<br />";
echo $stmt."<br /><br />";

$numFields = sqlsrv_num_fields( $stmt );
$numListen = sqlsrv_num_rows( $stmt ); 
$numAfect  = sqlsrv_rows_affected( $stmt );
echo "Total de linhas > ".$numListen." .... Total de colunas > ".$numFields." .... Total afetado > ".$numAfect."<br /><br /><br />";

if (!sqlsrv_execute($stmt)) {
    echo "Your code is fail!";
die;
}

if ($stmt = sqlsrv_prepare($conn, $sql)) {

        
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

              echo $row['DataReferencia']."<br />";

        } 
        
        
    } else {  
        echo "Statement could not be prepared.\n";  
        die(print_r(sqlsrv_errors(), true));  
    } 

Since it is displaying a normal query, it returns all correct values.

            $stmt =sqlsrv_query($conn,"SELECT * FROM usuario");

            while( $row = sqlsrv_fetch_array($stmt)){
                
                echo $row['login']."<br>";
            }
        

2 answers

2


try so friend.

$query = "EXEC PROCEDURE @CodigoEmpresa = ?, @DataReferencia = ? ";

$prepare = $con->prepare($query);

$prepare->execute();

while ($result = $prepare->fetch(PDO::FETCH_ASSOC)) {
print_r($result['XXXXXXXXX']."<br />"); // colunas a serem exibidas
}
  • Perfect, got it so thank you.

2

The connection I was able to do as follows

 try {
 $con = new PDO('sqlsrv:Server=SERVIDOR;Database=DB', 'USER', 'SENHA' );
 $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 } catch (Exception $ex) {
 print_r('Error: ' . $ex->getMessage());
 }

Now all that remains is to list the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.