如何从PHP调用存储过程并获得结果?

时间:2016-01-15 10:17:16

标签: php mysql

当我在MySQL控制台上运行以下程序test1时,我得到了

“”MySQL返回一个空结果集(即零行)。“

Delimiter $$
create procedure test1()
BEGIN
BLOCK1: begin
declare v_col1 int(10);                     
declare no_more_rows1 boolean default FALSE;  
declare cursor1 cursor for              
select content_id
from   topic_list where topic_id=1;
declare continue handler for not found  
set no_more_rows1 = TRUE;           
open cursor1;
LOOP1: loop
fetch cursor1
into  v_col1;
if no_more_rows1 then
close cursor1;
leave LOOP1;
end if;
BLOCK2: begin
   declare v_col2 int(10);
   declare no_more_rows2 boolean default FALSE;
   declare cursor2 cursor for
   select content_id
    from   content_upvotes
    where  u_id_upvoter = 1;
   declare continue handler for not found
   set no_more_rows2 = TRUE;
   open cursor2;
   LOOP2: loop
    fetch cursor2
    into  v_col2;
    if no_more_rows2 then
        close cursor2;
        leave LOOP2;
    end if;
    select count(*) as mynum from (SELECT *from content_upvotes where content_id=v_col1) t1 join (select u_id_upvoter as user_id from content_upvotes where content_id= v_col2) t2 on t1.u_id_upvoter=t2.user_id ;
end loop LOOP2;
end BLOCK2;
end loop LOOP1;
end BLOCK1;
end $$
DELIMITER ;

我如何从php调用它?所以我得到每个变量的输出mynum变量(v_col1,每个v_col2)变量

1 个答案:

答案 0 :(得分:0)

此存储过程不会重新生成任何结果集(行)。它有一个mynum out参数,因此它以这种方式传达结果。但是,您将只获得1个输出,而不是更多。如果需要返回多于1个数字,则需要重写存储过程。这是你唯一可以做的事情。

要从输出参数中获取数据,请使用以下sql语句:

call test1(@a); //@a is a mysql variable that will receive the output from the SP
select @a as mynum; //return the value of @a variable

select将返回一个带有1个字段(mynum)和1行的结果集:输出参数的值。您可以使用从php中选择的mysql客户端库执行上述语句。