PHP PDO cannot get OUT parameter value

前端 未结 1 1831
感动是毒
感动是毒 2021-01-13 08:56

I just started using PHP PDO with MySQL stored procedures and I have problem with how to get OUT parameters from the procedure call. I looked at many similar stackoverflow t

1条回答
  •  情书的邮戳
    2021-01-13 09:24

    The problem was that the first query that is calling the stored procedure is not considered as finished and closed, and PDO will not execute another query until the previous query is done.

    The solution was to add $proc->closeCursor();

    The whole working sample is:

    $input = 5;
    $mydb = new PDO("mysql:host=localhost;dbname=mydb", "user", "pass");
    $proc = $mydb->prepare("CALL proc_name($input, @o_code, @o_message)");
    $proc->execute();
    $proc->closeCursor();
    
    $output = $mydb->query("select @o_code, @o_message")->fetch(PDO::FETCH_ASSOC);
    var_dump($output); // array('@o_code'=>value, 'o_message'=>value)
    

    0 讨论(0)
提交回复
热议问题