I'm encountering a very interesting issue with a MySQL stored procedure. The procedure is as follows:
DROP PROCEDURE IF EXISTS `removeSubscription`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `removeSubscription`(IN `userId` int,IN `channelId` int,IN `channelTypeTitle` varchar(255))
BEGIN
SET @userId = userId;
SET @channelId = channelId;
SET @channelTypeTitle = channelTypeTitle;
DELETE FROM subscriptions
WHERE
userid = @userId AND
channelid = @channelId AND
channeltypeid = (SELECT id FROM channeltypes WHERE `name` = @channelTypeTitle)
LIMIT 1;
END
;;
DELIMITER ;
When this is called as a stored procedure from PHP, it is ignoring ALL of the 'WHERE' clause and is simply deleting the first row it encounters. This means that when the 'LIMIT 1' is left out, it deletes EVERYTHING from the table :s
This is the PHP:
$stmt = $db->prepare("CALL removeSubscription(:userId, :channelId, :channelTypeTitle)");
$stmt->bindValue('userId', $userId);
$stmt->bindValue('channelId', $channelId);
$stmt->bindValue('channelTypeTitle', $channelTypeTitle);
$stmt->execute();
Bizarrely enough, if I rename the passed parameters in both the PHP's prepare and in the Stored Procedure (for example, to have an 'x' before them), then it works correctly. Am I missing something obvious here?