php - 函数错误中的 MySQL 调用过程
问题描述
我正在尝试从程序结果中获取计数。我已经看到您可以创建一个临时表并稍后对其进行计数。但是当我将我的过程添加到函数中时,它停止工作。这是为什么?
$connection->query('
drop procedure if exists parents;
create procedure parents(in parent int(11),in name varchar(22))
begin
set @parent=parent;
drop temporary table if exists ids;
create temporary table ids(id int(11));
while @parent<>0 do
prepare statement from concat("select related into @parent from ",name," where id=?");
execute statement using @parent;
insert into ids(id) values(@parent);
end while;
select*from ids;
end;
drop function if exists counted;
create function counted(parent int(11),name varchar(22))
returns int(11)
begin
call parents(parent,name);
return (select count(*) from ids);
end;
');
$fetch=$connection->query('select counted(3,"category");')->fetchall();
我收到一个获取布尔错误。
解决方案
您不能在一次调用中执行多个查询query()
(您可以使用 来执行此操作multi_query()
,但您不能自定义查询分隔符以区分;
分隔查询和过程中使用的查询)。因此,将其分为多个查询。
$connection->query('xdrop procedure if exists parents');
$connection->query('
create procedure parents(in parent int(11),in name varchar(22))
begin
set @parent=parent;
drop temporary table if exists ids;
create temporary table ids(id int(11));
while @parent<>0 do
prepare statement from concat("select related into @parent from ",name," where id=?");
execute statement using @parent;
insert into ids(id) values(@parent);
end while;
select*from ids;
end;
');
$connection->query('drop function if exists counted');
$connection->query('
create function counted(parent int(11),name varchar(22))
returns int(11)
begin
call parents(parent,name);
return (select count(*) from ids);
end;
');
$fetch=$connection->query('select counted(3,"category");')->fetchall();
推荐阅读
- c - C程序调度器;如何使用 SetProcessAfinityMask() 将进程分配给特定的 CPU #
- android - 我需要从android设备打开一个图像,然后我必须调整它以使其更小,最后将它上传到firebase存储
- android - nativescript - firebase 插件的问题
- algorithm - 给定一组点,找出这些点是否存在一个凸四边形,除了它的角在里面之外没有其他点
- firebase - 使用 firebase 和应用引擎创建一次性 cron 作业
- python - 基本多层感知器中优化参数的问题
- node.js - 如果在 server.js 文件的 app.post 中的 python-shell 中更改,变量的值不会更改
- c++ - 如何修复“类模板的参数列表丢失”之类的错误消息?
- python - 安装 pysqlcipher3 的问题
- c++ - 当两种类型相同时,专门化一个 c++ 模板函数