Geoff_S
Geoff_S

Reputation: 5105

Getting output from stored procedure in PHP

I'm calling a stored procedure in php right now, and the third parameter of the procedure is an OUT param so it should be getting the output

When I run this:

function createNameRecord($firstName,$lastName){
    try{
        $sql = "CALL schema.CREATE_RECORD(?,?,?)";
        $values = [$firstName,$lastName,$returnID];
        return DB::connection('odbc')->select($sql,$values);
        dd($returnID);
    }catch(Exception $e){
        return false;
    }
}

The procedure runs properly and reloads the page, and when I check the database then the record was successfully created. However, I need the output variable which is the ID of the created record and I need it returned for a function that runs afterwards.

SO since this runs, successfully creates the record and reloads the page then how can I dump the output $returnID so that I can verify that it's storing my returned ID? The dump and die above doesn't work, potentially because of the page reload.

What's the best way to verify the return ID?

Upvotes: 0

Views: 123

Answers (2)

Blue
Blue

Reputation: 22921

As soon as your function sees return, the function will no longer execute any code after that line. In this case dd($returnID); will never get executed. If you're simply debugging, just remove return, and let dd() output/die:

function createNameRecord($firstName,$lastName){
    try{
        $sql = "CALL schema.CREATE_RECORD(?,?,?)";
        $values = [$firstName,$lastName,$returnID];
        DB::connection('odbc')->select($sql,$values);
        dd($returnID);
    }catch(Exception $e){
        return false;
    }
}

In this case, you're not caring about the return value (Whatever is calling createNameRecord), as you just want your script to end/die.

If it's still reloading the page, then your catch statement is likely being executed, and you should debug that as well:

function createNameRecord($firstName,$lastName){
    try{
        $sql = "CALL schema.CREATE_RECORD(?,?,?)";
        $values = [$firstName,$lastName,$returnID];
        DB::connection('odbc')->select($sql,$values);
        dd($returnID);
    }catch(Exception $e){
        dd($e);
    }
}

Upvotes: 1

Ηρακλής Β.
Ηρακλής Β.

Reputation: 84

I am new to the community and don't fully know how to guide you but what if you tried mysqli_real_query() and retrieved the results as the doc says:

Executes a single query against the database whose result can then be retrieved or stored using the mysqli_store_result() or mysqli_use_result() functions.

Upvotes: 2

Related Questions