doujingjiao0015 2019-01-27 02:36 采纳率: 0%
浏览 55

如何使用php pdo从mysql中的store-procedure / function获取返回值

I'm calling a simple MySQL store procedure that receives 2 parameters, but when I try to get the value returned from PDO PHP I get array(0){} as result in the var_dump. Also I've tried different ways to get the value and I get boolean value (always false). When I test store procedure in DB it works correctly.

Hope you can help me to find my error. Thank you

MySQL Store Procedure:

CREATE DEFINER=`root`@`localhost` FUNCTION `F_esUsuarioValido`(`USUARIO_IN` VARCHAR(50),`CONTRASENA_IN` VARCHAR(50))
RETURNS tinyint(4)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER

BEGIN
  DECLARE EXISTE INT;

  SELECT STATUS
  INTO EXISTE
  FROM T_USUARIO
  WHERE USUARIO = USUARIO_IN
  AND CONTRASENA = CONTRASENA_IN;

  IF EXISTE IS NULL THEN
    RETURN 0;
  ELSEIF EXISTE = 1 THEN
    RETURN 1;
  ELSEIF EXISTE = 0 THEN
    RETURN 2;
  END IF;
END

PHP Code:

    public function esUsuarioValido($usuarioIN, $contrasenaIN) {
       $con = new ConexionMySQL();
       $pdo = $con->conectar();

       $sql = 'CALL F_esUsuarioValido(?,?)';
       $stmt = $pdo->prepare($sql);
       $stmt->bindParam(1, $usuarioIN, PDO::PARAM_STR);
       $stmt->bindParam(2, $contrasenaIN, PDO::PARAM_STR);

       $stmt->execute();

       $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
       var_dump($result);
    }

The result I get is: array(0) {} Or bool(false) when I tried to retrive with: $result = $stmt->fetch(); var_dump($result);

  • 写回答

1条回答 默认 最新

  • douliao5942 2019-01-27 23:00
    关注

    I've solved this. Instead of creating a function in MySQL with default return I created it as a Store procedure with 2 param IN and 1 Param OUT and asign the value to this OUT param inside the procedure according to my conditions without return statement.

     CREATE DEFINER=`root`@`localhost` PROCEDURE `F_esUsuarioValido`(
         IN `USUARIO_IN` VARCHAR(50),
         IN `CONTRASENA_IN` VARCHAR(50),
         OUT `ES_VALIDO_OUT` TINYINT
     )
     LANGUAGE SQL
     NOT DETERMINISTIC
     CONTAINS SQL
     SQL SECURITY DEFINER
     BEGIN
         DECLARE EXISTE INT;
    
         SELECT STATUS
         INTO EXISTE
         FROM T_USUARIO
         WHERE USUARIO = USUARIO_IN
         AND CONTRASENA = CONTRASENA_IN;
    
         IF EXISTE IS NULL THEN
             SET ES_VALIDO_OUT = 0;
         ELSEIF EXISTE = 1 THEN
             SET ES_VALIDO_OUT = 1;
         ELSEIF EXISTE = 0 THEN
             SET ES_VALIDO_OUT = 2;
         END IF;
     END
    

    Then in PHP PDO I modified my statement to have 2IN and 1 OUT params, used PDO::bindParam to set only IN params and to retrieve the OUT param had to query a SELECT @OUTParam:

     public function esUsuarioValido($usuarioIN, $contrasenaIN) {
        $con = new ConexionMySQL();
        $pdo = $con->conectar();
    
        $sql = 'CALL F_esUsuarioValido(:usuario, :contrasena, @esValido)';
        $stmt = $pdo->prepare($sql);
    
        $stmt->bindParam(":usuario", $usuarioIN, PDO::PARAM_STR);
        $stmt->bindParam(":contrasena", $contrasenaIN, PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
    
        $row = $pdo->query("SELECT @esValido AS esValido")->fetch(PDO::FETCH_ASSOC);
        var_dump($row);
     }
    

    After that, var_dump($row) shows variable esValido with the correct value.

    This website helped me alot: http://www.mysqltutorial.org/php-calling-mysql-stored-procedures/

    To know the difference between a function and store procedure returning values this web site helped: https://www.quora.com/What-difference-between-stored-procedures-and-functions-in-MySQL

    Hope this answer can help many other people. Regards!

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计