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

如何在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"

  • 写回答

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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)