dtvfxzq3802 2012-02-07 19:14
浏览 31
已采纳

在php中创建用户定义的mysql函数

I have this problem whereby, i have created a user defined function within php. But it has some problems.Let me elaborate. suppose i've created this function

<?php

include 'db_connect.php';
$sql="DROP FUNCTION IF EXISTS testf";
$result=mysql_query($sql) or die (mysql_error());
$sql="CREATE  FUNCTION testf() RETURNS text
DETERMINISTIC
READS SQL DATA

BEGIN
DECLARE Output text;

set Output='zzz';

RETURN output ;
END";

$result=mysql_query($sql) or die (mysql_error());

$sql="SELECT testf()";
$result=mysql_query($sql) or die (mysql_error());
$row=mysql_fetch_array($result);
echo nl2br($row[0]);

?>

The above is working fine.

but the following has an error:

<?php
include 'db_connect.php';
$sql="DROP FUNCTION IF EXISTS testf";
$result=mysql_query($sql) or die (mysql_error());
$sql="CREATE  FUNCTION testf() RETURNS text
DETERMINISTIC
READS SQL DATA

BEGIN
DECLARE Output text;
DECLARE name text;
set Output='zzz';
set name='SELECT t_name for tbl_names where id=1';  
RETURN output ;
END";

$result=mysql_query($sql) or die (mysql_error());

$sql="SELECT testf()";
$result=mysql_query($sql) or die (mysql_error());
$row=mysql_fetch_array($result);
echo nl2br($row[0]);
?>

i'm getting this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT t_Name FROM tbl_name WHERE id=1; RETURN output ; END' at line 9 Can anyone suggest a solution?, i'm stuck. i know i could execute the query 'SELECT t_name for tbl_names where id=1' and pass the result as parameter. It would work yes, but i want to avoid it.

  • 写回答

2条回答 默认 最新

  • drmet46444 2012-02-07 19:28
    关注

    mysql_query doesn't allow but one query at a time. You're not going to be able to define any but the most trivial stored procedures with it, given that limitation.

    You generally shouldn't be using the mysql extension anyway -- it's ancient, and was made for versions of MySQL that didn't even have such things as stored procedures. Why it still exists is beyond me.

    Anyway, you could use the mysqli extension; there's even an example in the docs. It can be used quite similarly to the old procedural way, if you're more comfortable with that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址