如何在 php 中编写 mysql 用户定义函数

How to write mysql user defined function in php

请告诉我如何在 PHP...

中定义 MySQL 用户定义函数

我正在使用以下 code.but 它不起作用。

它抛出错误:

" (1313) RETURN is only allowed in a FUNCTION "

$mysqli = new mysqli( $DBHost, $DBUserName, $DBPassword, $DBName );


$Query =
"CREATE PROCEDURE FunGetRegistryValue(OUT Param_RegistryKey varchar(40))
BEGIN
    DECLARE vResult varchar(90);
    SELECT  iFnull(Value,'') INTO vResult FROM Project_Registry Where RegistryKey=Param_RegistryKey;
    RETURN(vResult);
END;";

if( !$mysqli->query( $Query ) ) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

当我使用 FUNCTION 关键字而不是 PROCEDURE 时,出现语法错误...

请给我 "VALUE RETURNING FUNCTION"

的示例代码
DELIMITER $$ 
CREATE DEFINER=root@localhost
FUNCTION FunGetRegistryValue(Param_RegistryKey varchar(40)) 
RETURNS varchar(90) 
CHARSET latin1 DETERMINISTIC 
BEGIN 
    DECLARE vResult varchar(90); 
    SELECT iFnull(Value,'') INTO vResult 
    FROM Project_Registry Where RegistryKey=Param_RegistryKey;
    RETURN(vResult); 
END$$ 
DELIMITER ;

最初是 Fozia 的评论。我将它添加为答案,这样它就不会在查询时变得混乱 运行 并且清晰易读。

$mysqli = new mysqli($DBHost, $DBUserName, $DBPassword, $DBName );

$Query =
"DROP FUNCTION IF EXISTS FunGetRegistryValue;

CREATE   FUNCTION FunGetRegistryValue(Param_RegistryKey VARCHAR(40)) RETURNS VARCHAR(90) CHARSET latin1
   DETERMINISTIC
BEGIN
   DECLARE vResult VARCHAR(90);
   SELECT  IFNULL(VALUE,'') INTO vResult FROM Project_Registry WHERE RegistryKey=Param_RegistryKey;
   RETURN(vResult);
   END;";

if( !$mysqli->multi_query( $Query ) ) {
    echo $mysqli->error;
    exit();
}