I just converted the following PHP code to a MySQL stored procedure. There is no explicit syntax error since I could execute it using PHPMyAdmin. I see it with
SELECT routine_definition FROM information_schema.routines WHERE routine_schema = 'chess';
Since this is the first time I wrote a stored procedure, I would like to know
- Does the stored procedure do what I think? (See the Flowchart in βWhat Should It Doβ)
- Is the stored procedure regular SQL (to some standard) or can I only use this with MySQL databases? What is MySQL? Can i get rid of this?
- Is there a way to improve this stored procedure? Are there any better methods that I violated?
- Do I need to sanitize input when using stored procedures?
Here is a quick overview of the database and all the code . But I hope there is no need to answer my questions.
What should he do

New stored procedure
DELIMITER // CREATE PROCEDURE ChallengeUser( IN challengedUserID INT, IN currentUserID INT, OUT startedGamePlayerUsername varchar(255), OUT startedGameID INT, OUT incorrectID BIT, OUT alreadyChallengedPlayer BIT, OUT alreadyChallengedGameID INT ) BEGIN SELECT `username` AS startedGamePlayerUsername FROM chess_users WHERE `user_id` = challengedUserID AND `user_id` != currentUserID LIMIT 1; IF startedGamePlayerUsername IS NOT NULL THEN SELECT `id` FROM `chess_games` WHERE `whiteUserID` = currentUserID AND `blackUserID` = challengedUserID AND `outcome` = -1 LIMIT 1; IF id IS NULL THEN SELECT `softwareID` AS `whitePlayerSoftwareID` FROM chess_users WHERE `user_id`=currentUserID LIMIT 1; SELECT `softwareID` AS `blackPlayerSoftwareID` FROM chess_users WHERE `user_id`=challengedUserID LIMIT 1; INSERT INTO `chess_games` (`tournamentID`, `whiteUserID`, `blackUserID`, `whitePlayerSoftwareID`, `blackPlayerSoftwareID`, `moveList`) VALUES (NULL, currentUserID, challengedUserID, whitePlayerSoftwareID, blackPlayerSoftwareID, ""); SELECT `id` AS startedGameID FROM chess_games WHERE `whiteUserID` = whitePlayerSoftwareID AND `blackUserID` = blackPlayerSoftwareID AND `whitePlayerSoftwareID` = whitePlayerSoftwareID AND `blackPlayerSoftwareID` = blackPlayerSoftwareID AND `moveList` = "" LIMIT 1; ELSE SET alreadyChallengedPlayer = 1; SET alreadyChallengedGameID = id; END IF; ELSE SET incorrectID = 1; END IF; END // DELIMITER ;
New PHP code
function challengeUser2($user_id, $t) { global $conn; $stmt = $conn->prepare("CALL ChallengeUser(?,?,@startedGamePlayerUsername,". ."@startedGameID,@incorrectID," ."@alreadyChallengedPlayer,@alreadyChallengedGameID)"); $test = USER_ID; $stmt->bindParam(1, $user_id); $stmt->bindParam(2, $test); $returnValue = $stmt->execute(); echo "Return Value\n"; print_r($returnValue); echo "################\n\nstmt\n"; print_r($stmt); echo "################\n\nrow\n"; $row = $stmt->fetch(PDO::FETCH_ASSOC); print_r($row); }
What does he print
Return Value 1
What should he do
He had to create a new entry in the chess_games table. But there is no new entry and there is no value for incorrectID or alreadyChallengedPlayer . Therefore, I think I made a mistake.