函数错误中的MySQL调用过程

时间:2018-05-01 21:22:34

标签: php mysql stored-procedures mysqli

我试图从程序结果中获取计数。我已经看到你可以创建一个临时表并在以后计算它。但是当我将我的程序添加到该函数时,它将停止工作。那是为什么?

  $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();

我得到一个fetch布尔错误。

1 个答案:

答案 0 :(得分:1)

您只需拨打query()一次即可执行多个查询(您可以使用multi_query()执行此操作,但无法自定义查询分隔符以区分{ {1}}分隔查询和过程中使用的查询。因此将其拆分为多个查询。

;