dongxu198714
2015-03-27 11:19
浏览 89
已采纳

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

Please tell me how to define MySQL User Defined Function with in PHP...

I'm using following code.but it is not working.

it throws error :

" (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;
}

When i use FUNCTION keyword instead of PROCEDURE then i get syntax error...

Please give me example code for "VALUE RETURNING FUNCTION"

图片转代码服务由CSDN问答提供 功能建议

请告诉我如何在PHP中定义MySQL用户自定义函数...

我正在使用以下代码。但它无效。

它会抛出错误:

“(1313 )RETURN仅允许在功能“

  $ 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其中RegistryKey = Param_RegistryKey  ; 
 RETURN(vResult); 
END;“; 
 
if(!$ mysqli-> query($ Query)){
 echo”存储过程创建失败:(“。$ mysqli-> errno。  “)”。  $ mysqli->错误; 
} 
   
 
 

当我使用FUNCTION关键字代替PROCEDURE时,我得到语法错误... \ n

请给我一个“VALUE RETURNING FUNCTION”的示例代码

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshang1934 2015-06-12 06:57
    已采纳

    $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();
    }
    
    点赞 打赏 评论
  • dqg17080 2015-06-10 21:27
    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 ;
    

    originally a comment by Fozia. I added it as an answer so that it's not a jumbled run on query and is legible.

    点赞 打赏 评论

相关推荐 更多相似问题