Using pdo in php with stored procedure

后端 未结 2 609
栀梦
栀梦 2020-12-16 04:48

I have a simple stored procedure in MySQL database:

DELIMITER $$
CREATE DEFINER=`vidhu`@`%` PROCEDURE `test`(var_datain TEXT)
BEGIN
    SELECT var_datain;
EN         


        
相关标签:
2条回答
  • 2020-12-16 05:12

    The following code works for calling without prepare statement!

    $query="CALL store_procedure_name(@a)";
    $conn->query($query);
    
    $query="SELECT @a as outvar;";
    $result = $conn->query($query);
    foreach ($result as $x)
    {
        $res=$x['outvar'];
    }
    
    echo $res;
    
    0 讨论(0)
  • 2020-12-16 05:15

    You need to use bindValue instead of bindParam.

    When you use bindParam, it binds the variable provided to the parameter, not the value of the variable.

    So, if you do:

    $x = 5;
    $stmt->bindParam(1, $x, PDO::PARAM_INT);
    $x = 6;
    $stmt->execute(); //executes with 6 instead of 5
    

    It's actually executed with 6 rather than 5. To do this, the method must have a reference to the variable. You cannot have a reference to a literal, so this means that bindParam cannot be used with literals (or anything you can't have a reference to).

    $x = 5;
    $stmt->bindValue(1, $x, PDO::PARAM_INT);
    $x = 6;
    $stmt->execute(); //executes with 5 instead of 6
    

    Then:

    $stmt->bindParam(1, 1, PDO::PARAM_INT); 
    //invalid because there's no way to pass a literal 1 by reference
    $stmt->bindValue(1, 1, PDO::PARAM_INT);
    //valid
    
    0 讨论(0)
提交回复
热议问题