test1是在传递两个变量v_col1和v_col2之后计算正在创建的表的行的过程。当我在MySQL上运行它时,显示
" MySQL返回一个空结果集(即零行)。 (查询耗时0.0020秒)"
然而我无法从php调用此功能。
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 ;
请帮帮我。
答案 0 :(得分:0)
首先,观察(这不是你问的问题的答案)......
这个程序似乎过度复杂,无论它应该是什么样的。
问:此程序会计算行数吗?
答:如果您询问此程序是否会返回"行数" ...
有条件地,在cursor1
返回至少一行的情况下,cursor2
在执行过程期间的某个时刻返回至少一行。回答你的第一个问题...
是的,可能mynum
(程序的输出参数)将填充"行数"。
"计数"返回将来自select count(*)
查询的最后执行(在loop2的底部)。 <{1}}查询先前执行的结果将被丢弃。
如果select count(*)
没有返回一行,那么答案是否,该程序将不会返回任何&#34;行数&#34;。此外,如果永远不会从cursor1
返回一行,则答案也是否。
问:如何从PHP调用此过程?
答:假设您使用的是PDO,可以使用这样的模式:
cursor2
为了跟进我最初的观察......我对这个程序存在的任何正当理由感到困惑。
<强>后续强>
上面的代码示例解决了具有<?php
$sth = $dbh->prepare("CALL test1(?)");
$sth->bindParam(1, $return_val, PDO::PARAM_INT, 20);
$sth->execute();
print "call to msyql stored procedure test1 returned $return_val\n";
?>
参数的原始过程定义。问题中的过程定义已更改为删除OUT
参数,并且OUT
SQL语句已更改为删除SELECT ... INTO var
。修改后的过程有可能返回零,一个或多个结果集。
要执行该过程(没有参数)并从过程中检索所有结果集,您可以使用如下模式:
INTO var