Calling Stored Procedure in foreach loop - only first executed

后端 未结 2 1778
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 22:39

This is really derivative of my earlier question today.

I created a Stored Procedure in my database that I wanted to call several times in a row from PHP.
Let\'s say

相关标签:
2条回答
  • 2021-01-23 22:47

    A stored procedure may return more than one result set and it will always return one extra result set that does not contain any data but the overall error/warning information about the procedure call in addition to any explicitly returned result sets.

    source - http://forums.mysql.com/read.php?52,228296,228347#msg-228347

    0 讨论(0)
  • 2021-01-23 23:00

    In php, when we call a stored procedure in a loop, it just execute it once. It happens when the stored procedure returns any result set. I faced the same issue. I had a stored procedure for updating table records.

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `espritkm`.`update_notification`$$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `update_notification`(item_id_val VARCHAR(11),item_source_val VARCHAR(50),item_type_id_val INT(50),item_type_val VARCHAR(50),created_at_val BIGINT(11),pivot_user_id_val VARCHAR(256),pivot_item_type_val VARCHAR(64),pivot_owner_type_val VARCHAR(64),pivot_owner_id_val INT(11),user_id_val VARCHAR(64),  OUT row_effect VARCHAR(11))
    Begin
      Declare item_count INT(10);
       SET @SQL1 = CONCAT('select count(*) into @item_count from item_notifications where item_id = ''', item_id_val, '''');                                                                                    PREPARE S1 FROM @SQL1;                                                                                EXECUTE S1;                                                                                    DEALLOCATE PREPARE S1;     
        IF @item_count = 0 THEN
           SET @SQL2 = CONCAT('INSERT INTO item_notifications (item_id,item_source,item_type_id,item_type,created_at,pivot_user_id,pivot_item_type,pivot_owner_type,pivot_owner_id,user_id) value(''',item_id_val,''',''',item_source_val,''',''',item_type_id_val,''',''',item_type_val,''',''',created_at_val,''',''',pivot_user_id_val,''',''',pivot_item_type_val,''',''',pivot_owner_type_val,''',''',pivot_owner_id_val,''',''',user_id_val,''')');    
     PREPARE S2 FROM @SQL2;                                                                                  EXECUTE S2;                                                                                     DEALLOCATE PREPARE S2;      
          SET row_effect= "Insert";    
        ELSE             
            SET row_effect= "Update";    
    
        SET @SQL3 = CONCAT('UPDATE item_notifications SET viewer_id = ''',user_id_val,''' WHERE item_id = ''' ,item_id_val,'''') ;     
           PREPARE S3 FROM @SQL3;                                                                                 EXECUTE S3;                                                                                    DEALLOCATE PREPARE S3;      
    
        END IF;
        SELECT row_effect;
    END$$
    
    DELIMITER ;
    

    And this was supposed to be executed for 1000+lines, but executed for just one record.

    It does not support in case of when your SP return any dataset. Just eliminate the OUT var, or any select statement(just for any result reference), and it'll work fine.

    0 讨论(0)
提交回复
热议问题