php 8.1 + mysql 5.7:存储过程中的错误处理程序不起作用

问题描述 投票:0回答:1

我有一个存储过程,我尝试将一些数据插入两个表中,但我不想插入重复的键。所以我编写了以下过程来处理错误1062:重复键。这是代码:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` 
PROCEDURE `insert_profilo_staff`(IN `id_persona` INT(11) UNSIGNED, IN `id_staff` VARCHAR(15) CHARSET utf8, IN `id_ruolo` VARCHAR(30) CHARSET utf8, IN `id_zona` VARCHAR(15) CHARSET utf8, IN `id_territorio` VARCHAR(15) CHARSET utf8, IN `gerarchia` INT(5) UNSIGNED, IN `ambito_giurisdizionale` VARCHAR(15) CHARSET utf8)
    MODIFIES SQL DATA
BEGIN
 
  
  DECLARE EXIT HANDLER FOR 1062 ROLLBACK;
  BEGIN
     SIGNAL SQLSTATE '23000' SET MESSAGE_TEXT = "Il profilo che si sta cercando di inserire e' già associato all'utente.";

  START TRANSACTION; 
  
  IF gerarchia < 10000 THEN
    
      INSERT INTO tb_utenti_profili SET 
        id = id_persona,
        staff = id_staff, 
        ruolo = id_ruolo, 
        id_zona = id_zona;
        
     ELSE
     
      INSERT INTO tb_utenti_profili SET 
        id = id_persona,
        staff = id_staff, 
        ruolo = id_ruolo, 
        id_zona = id_zona;        
        
       INSERT INTO tb_utenti_territori_supervisori SET 
        id = id_persona,
        staff = id_staff, 
        id_territorio = id_territorio; 
        
     END IF;    
      -- SET @erroreOut = '00000';
     COMMIT;
     
   END;   
END$$
DELIMITER;

然后我在 try catch 块内使用 msqli 执行该过程,如下所示。但即使我将不存在的 n-uple 插入数据库中,我总是会收到在 catch 块内管理的错误。怎么了?

   $db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
   $is_exception = 0;

   try {
          $query = "CALL insert_profilo_staff( ?, ?, ?, ?, ?, ?, ? ) ";
          $stmt = $db_connection->prepare($query);
          $stmt->bind_param("issssis", $inp_id_persona, $inp_id_staff, $inp_id_ruolo, $inp_id_zona, $inp_id_territorio, $inp_gerarchia, $inp_ambito_giurisdizionale);

          $inp_id_persona =  intval(trim($_POST['id_persona'] ?? ''));
          $inp_id_staff = trim($_POST['id_staff'] ?? '');
          $inp_id_ruolo = trim($_POST['id_ruolo'] ?? '');
          $inp_id_zona = trim($_POST['id_zona'] ?? '');
          $inp_id_territorio = trim($_POST['id_territorio'] ?? '');
          $inp_gerarchia = intval(trim($_POST['gerarchia'] ?? ''));
          $inp_ambito_giurisdizionale = trim($_SESSION['kaikan'] ?? '');

          $stmt->execute();
      }
      catch(Exception $e)
      {
            error_log( "l eccezione: " . $e->getMessage() );
            $is_exception = 1;
            error_log( "Call procedure insert_profilo_staff failed ");
            error_log( "Stato errore: " . $db_connection->sqlstate . " ");
            error_log( "N.ro errore: ". $db_connection->errno . " ");
            error_log( "Errore: " . $db_connection->error . " ");

            $result = 'error';
            $message= "" . $db_connection->error . " ";
            $mysql_data = [];
      }
      finally {
         $stmt->close();
         $db_connection->close();
         $query = "";
      }

        if ($is_exception == 0)
        {
           $result='success';
           $message='OK';
           $mysql_data = [];
        }

    $data = array(
        "result"  => $result,
        "message" => $message,
        "data"    => $mysql_data
        );

    // Convert PHP array to JSON array
    $json_data = json_encode($data);
exception error-handling insert try-catch on-duplicate-key
1个回答
0
投票

我用下面的代码行解决了这个问题,其中我用异常的错误号来评估 errno 变量,然后在返回 Php 时读取它。正如以下链接所建议的:

https://stackoverflow.com/a/46125452

声明 errno INT; 声明 SQLEXCEPTION 的退出处理程序 开始 获取当前诊断条件 1 errno = MYSQL_ERRNO; 选择错误号作为 MYSQL_ERROR; 回滚; 结束;

© www.soinside.com 2019 - 2024. All rights reserved.