简体   繁体   中英

how to get return value from store-procedure/function in mysql with php pdo

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);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM