query in PHP is empty but in SQL Server it works

Asked

Viewed 147 times

1

Good afternoon, I have a procedure to run on sql server, if I run directly on SQL it runs normally, returning me the expected line as in the image below:

inserir a descrição da imagem aqui

but when I run codeIgniter 3 (php) it doesn’t error but also brings my Row.

controller:

 $result = $this->posvendas->totalPassagens([getCodeLocal($local), date('Y-m-01 00:00:00'), date('Y-m-d 23:59:59')]); // Não esta retornando a consulta
         echo '<prev>';
         print_r($result);

Model:

     public function totalPassagens(array $array)
{
    $params = [
        3562,
        0,
        '2020-10-01 00:00:00',
        '2020-10-30 23:59:59'
    ];

    $sql = "SET NOCOUNT ON exec whRelOSTotalizarResultadoAgendamento ?, ?, ?, ?";
    $data = $this->db->query($sql, $params);
    return $data->result_array();
}

Exit:

inserir a descrição da imagem aqui

I don’t understand why not return the results

  • The answer answered your question?

2 answers

2

To succeed in sending values to a Stored Procedure has to pass a array of parameters, minimum example:

Table:

inserir a descrição da imagem aqui

Stored Procedure

ALTER PROCEDURE [dbo].[SP_INSERT_SOURCES]   
    @Name VARCHAR(50),
    @Created Date,
    @Status Bit
AS
BEGIN   
    SET NOCOUNT ON;
    INSERT INTO Sources(Name, Created, Status) VALUES(@Name, @Created, @Status);
    SELECT * FROM Sources WHERE Sources.Id = SCOPE_IDENTITY();
END

that script has the function to insert new record, retrieve and send the entered record to the code.


How to use it all now with ?

Sample code:

$params = [
    'StackOverFlow',
    '1992-01-01',
    0
];
$sql = 'EXEC SP_INSERT_SOURCES ?, ?, ?';
$this->db->query($sql, $params);

in the parameters the question mark (?) refers to the amount of variables Stored Procedure.

In your code basically:

public function totalPassagens()
{
    $params = [
        3562,
        0
        '2020-10-01 00:00:00',
        '2020-10-30 23:59:59'
    ];
    $sql = "exec whRelOSTotalizarResultadoAgendamento ?, ?, ?, ?";
    $query = $this->db->query($sql, $params);
    return $query->result_array();
}

Observing: respect the type of information sent to the database, that is, how each field is configured.

References:

  • i did exactly as you told me, passing parameters, but the result is the same, I put exactly the entire query without parameters to make sure it is not passing wrong value and causing error, but the problem is returning the values

  • This whole answer is tested and evaluated by me because it’s code that helps not just you, but everyone, you who’s doing something wrong, and something else that’s wrong? or you do not touch PHP is beginner? @LEANDRODASILVA

  • am beginner yes, can I delete this question? or the other, I thought the other is more complete! I will edit the other!

  • Delete is described simply no one will be answering your questions @LEANDRODASILVA because this answers your new question.

  • I did exactly as you said, I’m taking the same error, it can not be sql syntax error because in another test it was and in the proper sqlserver works, could not be the way to return the values?

  • Look again at the method example totalPassagens() that is in the answer and test again @LEANDRODASILVA

  • Yes, I edited my question, unfortunately I am taking the same error, the funny thing is that and any PHP file I did a test and it was, only in CI3 does not bring back the result!

Show 2 more comments

1

Good afternoon, I managed to solve as follows, inside the Procedure SET NOCOUNT ON; after Begin instead of putting before the exec command) I could put the sql code here but there are more than 240 lines I believe that is not the case now:

begin  
  SET NOCOUNT ON;

Browser other questions tagged

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