How to execute a stored procedure in php using sqlsrv and “?” style parameters

前端 未结 3 832
天涯浪人
天涯浪人 2020-11-29 11:34

I\'ve looked over several other questions that seem (from the titles) the same as this. However, my case is a bit different.

The following works (i.e. I get \"succes

3条回答
  •  爱一瞬间的悲伤
    2020-11-29 11:54

    This is a follow up to the answer by @chris85.

    It's worth noting here that once the statement is prepared, you need to execute it:

    $sql = "EXEC stp_Create_Item @Item_ID = ?, @Item_Name = ?";
    $stmt = sqlsrv_prepare($conn, $sql, $procedure_params);
    if (!sqlsrv_execute($stmt)) {
        echo "Your code is fail!";
        die;
    }
    while($row = sqlsrv_fetch_array($stmt)){
        //Stuff
    }
    

    sqlsrv_execute() only returns true/false. If you want to parse the data returned by the stored procedure you can process it just like the result from sqlsrv_query().

    If you forget the sqlsrv_execute() you'll get an error saying that the result has to be executed before it can be used.

提交回复
热议问题