iT邦幫忙

0

MySQL procedure 在 php 可以取值嗎

假設我今天寫了個 procedure 叫 test()

delimiter $$
drop procedure if exists test||
create procedure test()
begin
select * from table;
end
$$
delimiter ;

我知道在 php 呼叫的方式是

$query = mysql_query('call test()');

但我想要的是這個結果就像我下 query 一樣讓我可以 fetch

$sql = "select * from table";
$query  = mysql_query($sql);
$row = mysql_fetch_assoc

while(.....

請問該怎麼做呢?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
builder
iT邦新手 1 級 ‧ 2011-09-03 21:23:13
最佳解答

//就照原本的方式 fetch 出資料
$sql = "call test()";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
while(.....

看更多先前的回應...收起先前的回應...
chan15 iT邦新手 2 級 ‧ 2011-09-03 21:36:35 檢舉

不行耶,會顯示
supplied argument is not a valid MySQL result resource

wiseguy iT邦超人 1 級 ‧ 2011-09-04 18:15:44 檢舉

請在 $query = mysql_query($sql); 下面插一行
if (mysql_errno()) die(mysql_error());
把錯誤印出來看看吧 (如果有錯的話)
因為 supplied argument is not a valid MySQL result resource 這種訊息通常是 SQL 或 procedure 根本就寫錯了。

chan15 iT邦新手 2 級 ‧ 2011-09-07 02:00:58 檢舉

PROCEDURE test.test can't return a result set in the given context

chan15 iT邦新手 2 級 ‧ 2011-09-07 02:05:41 檢舉

在網路找到這篇解答,call procedure 返回值這麼麻煩喔
http://hi.baidu.com/zhujinyu/blog/item/231b6fa9e21126f11e17a2c0.html

2
wiseguy
iT邦超人 1 級 ‧ 2011-09-03 00:18:21

就把
$sql = "select * from table";
換成
$sql = "call test()";
就這樣。

不過假如你有多個 procedure 要連續 call,請在 query 下一個 procedure 前,先下一個 next_result(),以免出現 "Commands out of sync; you can't run this command now" 錯誤。

另外,建議使用 mysqli 系列指令,不要再用 mysql 系列了。

chan15 iT邦新手 2 級 ‧ 2011-09-03 02:22:47 檢舉

請問這樣以後要怎麼在 PHP fetch 出資料呢?

wiseguy iT邦超人 1 級 ‧ 2011-09-04 18:13:17 檢舉

就跟你原本下 SQL 指令一樣的 fetch 囉

我要發表回答

立即登入回答