使用mysql存储过程更新多行

时间:2015-04-07 11:07:43

标签: php mysql stored-procedures

$UserId = $obj ["userid"];
$CalendarId = $obj ["arrayOfCalendarIdInStrings"];
$Calendars = explode ( ",", $CalendarId );
$Count = count ( $Calendars );
for($i = 0; $i < $Count; $i ++) {
                        // Update Query
            $query = "UPDATE calendars SET `isSelected` = '1' WHERE `calendar_id` = '$Calendars[$i]' AND  user_id='$UserId'";
            $update = mysql_query ( $query );
        }

如何转换为Mysql存储过程

1 个答案:

答案 0 :(得分:0)

将查询转换为存储过程:

DELIMITER //

DROP PROCEDURE IF EXISTS myProcedure//
CREATE PROCEDURE myProcedure (
  IN vCalendarID int(11),
  IN vUserID int(11)
)

BEGIN

 UPDATE calendars SET `isSelected` = '1' WHERE `calendar_id` = vCalendarID AND  user_id = vUserID;

END//

DELIMITER ;

然后在您的PHP脚本中,您需要执行以下操作:

$query = "CALL myProcedure('".$Calendars[$i]."','".$UserId."');";
$update = mysql_query ( $query );

请注意,mysql_*函数已弃用。请改用mysqli或PDO。