MySQL 存储过程不能与 SELECT 一起使用(基本问题)
我正在使用一个平台(perfectforms),它要求我对大多数查询使用存储过程,并且从未使用过存储过程,我无法弄清楚我做错了什么。以下语句执行时没有错误:
DELIMITER //
DROP PROCEDURE IF EXISTS test_db.test_proc//
CREATE PROCEDURE test_db.test_proc() SELECT 'foo'; //
DELIMITER ;
但是当我尝试使用以下方式调用它时:
CALL test_proc();
我收到以下错误:
#1312 - PROCEDURE test_db.test_proc can't return a result set in the given context
我正在从 phpmyadmin 3.2.4、PHP 版本 5.2.12 中执行这些语句,并且 mysql 服务器版本为 5.0.89-community 。
当我编写一个返回参数的存储过程,然后选择它时,一切工作正常(例如):
DELIMITER //
DROP PROCEDURE IF EXISTS test_db.get_sum//
CREATE PROCEDURE test_db.get_sum(out total int)
BEGIN
SELECT SUM(field1) INTO total FROM test_db.test_table;
END //
DELIMITER ;
工作正常,当我调用它时:
CALL get_sum(@t); SELECT @t;
我得到总和没有问题。
最终,我需要做的是将一个精美的 SELECT 语句包装在存储过程中,这样我就可以调用它,并返回多个字段的多行。现在我只是想让任何选择工作。
非常感谢任何帮助。
I am using a platform (perfectforms) that requires me to use stored procedures for most of my queries, and having never used stored procedures, I can't figure out what I'm doing wrong. The following statement executes without error:
DELIMITER //
DROP PROCEDURE IF EXISTS test_db.test_proc//
CREATE PROCEDURE test_db.test_proc() SELECT 'foo'; //
DELIMITER ;
But when I try to call it using:
CALL test_proc();
I get the following error:
#1312 - PROCEDURE test_db.test_proc can't return a result set in the given context
I am executing these statements from within phpmyadmin 3.2.4, PHP Version 5.2.12 and the mysql server version is 5.0.89-community.
When I write a stored procedure that returns a parameter, and then select it, things work fine (e.g.):
DELIMITER //
DROP PROCEDURE IF EXISTS test_db.get_sum//
CREATE PROCEDURE test_db.get_sum(out total int)
BEGIN
SELECT SUM(field1) INTO total FROM test_db.test_table;
END //
DELIMITER ;
works fine, and when I call it:
CALL get_sum(@t); SELECT @t;
I get the sum no problem.
Ultimately, what I need to do is have a fancy SELECT statement wrapped up in a stored procedure, so I can call it, and return multiple rows of multiple fields. For now I'm just trying to get any select working.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
想通了。这不是 PHP 的错误(尽管曾经是)——这是某些版本的 phpmyadmin 中的错误。同样的错误间歇性地重新出现,然后在各种颠覆中得到修复(见上文):
此行为似乎仅限于 phpmyadmin 内存储过程中的 SELECT 语句。
使用像 MySQL Workbench 这样的客户端可以解决这个问题(或者你可以升级 phpmyadmin,但如果你像我一样使用共享服务器,那就很痛苦)。
无论如何,感谢大家的帮助。
Figured it out. This is not a bug with PHP (though it used to be) - it's a bug in some versions of phpmyadmin. The same bug intermittently reappears and is then fixed in various subversions (see above):
This behavior appears limited to SELECT statements within stored procedures inside phpmyadmin.
Using a client like MySQL Workbench works around the problem (or you could upgrade phpmyadmin, but that's a pain if you're on a shared server like I am).
Anyway, thanks to everyone for your help.
检查您的 php 版本,看看这是否是报告的错误(请参阅此处) 。
参见这篇文章:
无法在给定上下文中返回结果集
Check your php version to see if this is a reported bug (see here).
See this post:
Can't return a result set in the given context
这可能集中在处理存储过程/例程中的 PHP 和 SELECT 的错误上。一种可能的解决方法是使用 SELECT ... INTO 存储过程内部,将结果归结为一个变量。这在我读到的一些其他情况中有效。
This probably is centered around a bug dealing with PHP and SELECT within a stored procedure/routine. One possible work around is to use the SELECT... INTO inside of the stored procedure to bring the result down to one variable. This worked in some other cases that I read.
当我执行以下命令时:
接下来,
我向我显示一个结果集,例如:
我正在使用 PHP 5.3.1、MySQL 5.1.41 和 phpMyAdmin 3.2.4。
也许您只是拼错了程序名称?
或者您的 PHP 或 MySQL 安装有问题吗? (也许是一个错误。您是否尝试更新到最新版本?)
When I execute the following:
Followed by
I shows me a result set like:
I'm using PHP 5.3.1, MySQL 5.1.41 with phpMyAdmin 3.2.4.
Perhaps you just misspelled your procedure name?
Or could there be a problem with your PHP or MySQL installations? (Perhaps a bug. Have you tried updating to the latest versions?)