2013-01-03 79 views
1

我試圖讓這樣一個存儲過程:使用存儲過程從數據庫中進行選擇?

DELIMITER $$ 
DROP PROCEDURE IF EXISTS get_userStatUser $$ 
CREATE PROCEDURE get_UserStatUser 
(
IN userId INT, 
OUT Played INT, 
OUT Win INT, 
OUT Points INT 
) 
BEGIN 
SELECT played, win, points 
INTO Played, Win, Points 
FROM nf_users 
WHERE id = userId; 
END $$ 
DELIMITER ; 

我再嘗試從它那裏得到的數據是這樣的:

$sql = mysqli_query($connect,"CALL get_userStatUser()") or die("Query fail: " . mysqli_error()); 
$row = mysqli_fetch_array($sql); 

$played = $row['Played']; 
$win = $row['Win']; 
$points = $row['Points']; 

但有些事情是不對的。我是「新手入門程序」,看不到我出錯的地方?我得到「查詢失敗:」沒有任何錯誤?

任何幫助apreciated並在此先感謝:-)

+1

您**必須**將數據庫句柄傳遞給['mysqli_error'](http://php.net/mysqli_error)。您應該看到有關此錯誤或警告。請確保您的錯誤報告一直到-1。 – Charles

+1

檢查調用語句與proc定義的大小寫。他們不匹配。 – ethrbunny

回答

1

存儲過程需要4個參數

IN userId INT, 
OUT Played INT, 
OUT Win INT, 
OUT Points INT 

鑑於你的PHP調用存儲過程的方式,你需要重新定義如下:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS get_userStatUser $$ 
CREATE PROCEDURE get_UserStatUser 
(
IN userId INT 
) 
BEGIN 
SELECT played, win, points 
FROM nf_users 
WHERE id = userId; 
END $$ 
DELIMITER ; 

你的PHP

$given_userid = 372987; 
$sql = mysqli_query($connect,"CALL get_userStatUser($given_userid)") or die("Query fail: " . mysqli_error()); 
$row = mysqli_fetch_array($sql); 

$played = $row['played']; 
$win = $row['win']; 
$points = $row['points']; 

試一試!

+0

超級當然...做了這份工作......謝謝;-) – Mansa

相關問題