我试图创建这样的存储过程:
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'];
但有些事情不对。我对存储过程很陌生,看不出哪里出错了?我得到“查询失败:”没有任何错误?
如有任何帮助,请提前表示感谢
最佳答案
存储过程需要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'];
试试看!!!