PHP Sqlserver Stored Procedure does not run correctly

Asked

Viewed 809 times

3

Hello, I am developing an application using Laravel Framework Handler from my Backend and found the following error.

My Sqlserver Express 2008 R2 database has a stored Procedure that performs a series of routines and inserts the data already formatted into a table. stored Procedure receives a Key (Varchar) and the year (int) of the report I want to generate.

When I run a stored Procedure by Database script the insertion occurs correctly and I have all the data.

exec indicador.usp_indicador_101_1 'asdfgh', 2015

After running the process I run a method that performs the following select:

select * from indicador.T101_Indicador where A101_chave = 'asdfgh'

In the ideal scenario I have all results returned correctly: inserir a descrição da imagem aqui

But when I run the same code in PHP:

<?php
$server = "###.###.###.#";
$options = array(  "UID" => "usuario",  "PWD" => "senha",  "Database" => "meubanco");
$conn = sqlsrv_connect($server, $options);  

// Then execute the procedure
$proc = "exec indicador.usp_indicador_101_1 '147896311212', 2015";
$proc_result = sqlsrv_query($conn, $proc );
// Etc...
//mssql_free_statement($proc);
sqlsrv_close($conn);
?>

I have just the return of a line: inserir a descrição da imagem aqui

Stored Procedure makes the inserts by itself, the select of the data is done at another time. The photos shown are of selects made in the database script.

I tried in several ways the execution of stored Procedure, my last attempt was this with pure PHP and the return is always the same, only one line.

Selection in the application is done by a method using the Laravel features:

public static function T101_indicador($chave){

$dados = T101_indicador::where('A101_Chave', '=', $chave)
->orderBy('A101_Nomord')
->get();

//dd($dados);

return $dados;
}
  • There’s nothing missing from that code? Wouldn’t there be a fetch_array() thereabout?

  • The process itself has no return, I run a select in the table later to access the data and I believe that there I should use the fetch_array().

  • So the problem is in select and not in SP, put there in the question the rest of the code.

  • I ran this code and gave the select in the Database script and it returns only one line.

1 answer

0

I solved my problem, follow the new code:

$chave = (string) $chave;

$server = "enderecobanco";
$options = array(  "UID" => "usuario",  "PWD" => "senha",  "Database" => "bancodedados");
$conn = sqlsrv_connect($server, $options);

// Then execute the procedure
$proc = "{call $procedure(?,?)}";
  $params = array(&$chave, &$argumento);
  $statement = sqlsrv_prepare( $conn, $proc, $params);

sqlsrv_execute($statement);//Precisa executar Duas vezes para gerar todos os dados.
sqlsrv_execute($statement);

sqlsrv_close($conn);//fecha conexão

it was necessary to add "&" before the parameter array, according to this website and use the sqlsrv_execute() method twice instead of sqlsrv_query(). I don’t understand why double execution is necessary. Without this double execution the code does not have the correct result.

Browser other questions tagged

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