2016-01-15 83 views
-1

當我運行MySQL控制檯上執行以下步驟爲test1,我得到如何從PHP調用存儲過程並獲得結果?

「」 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)變量

+0

downvoted你只是因爲你在SO上註冊了2個帳戶,然後重複了這個問題:http://stackoverflow.com/questions/34816018/would-this-procedure-calculate-the-no-of-rows-how-do -i-呼叫此-過程往復 – crafter

回答

0

此存儲過程不會返回任何結果集(行)。它有一個mynum輸出參數,所以它通過結果的方式。只會得到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 

的選擇會返回一個結果以1場(myNum的)和1行:輸出參數的值。你可以用你從php中選擇的mysql客戶端庫執行上述語句。

相關問題