개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.04.11 23:35

MySQLi::autocommit() 사용시 문제가 발생하는 경우

목록
  • 자료구조마법사 1일 전 2025.04.11 23:35
  • 10
    1
MySQLi::autocommit() 사용시 문제가 발생하는 경우

안녕하세요. 저는 MySQLi::autocommit() 함수를 사용하여 트랜잭션을 관리하고 있습니다. 하지만, autocommit이 true로 설정되어 있는 상태에서 commit() 함수를 호출하면 에러가 발생하는 것을 확인했습니다.

이런 문제가 발생하는 이유는 무엇이며, autocommit이 true일 때 commit() 함수를 호출하는 방법은 무엇입니까?

해당 함수를 호출하는 방법에 대한 예시는 다음과 같습니다.

php

$conn = mysqli_connect("localhost", "root", "");



mysqli_select_db($conn, "test");



mysqli_autocommit($conn, true);



mysqli_query($conn, "INSERT INTO test_table (col1) VALUES ('value1)");



if (mysqli_autocommit($conn, false)) {

    $result = mysqli_commit($conn, true);

    if (!$result) {

        echo 'Error: " . mysqli_error($conn);

    }

} else {

    echo "Error: " . mysqli_error($conn);

}



위의 코드는 autocommit이 true일 때 commit() 함수를 호출하려고 하는데, 에러가 발생하는 것을 볼 수 있습니다.

해당 문제를 해결하기 위해 도와주세요.

    댓글목록

    profile_image
    나우호스팅  1일 전



    MySQLi::autocommit() 사용시 문제가 발생하는 경우는 다음과 같습니다.

    - autocommit이 true일 때 commit() 함수를 호출하면 에러가 발생합니다. 이는 autocommit이 true일 때 commit() 함수가 필요하지 않기 때문입니다. autocommit이 true일 때는 각 쿼리가 자동으로 커밋되기 때문입니다.

    autocommit이 true일 때 commit() 함수를 호출하는 방법은 다음과 같습니다.

    - autocommit을 false로 설정한 후 commit() 함수를 호출합니다.

    #hostingforum.kr
    php
    
    $conn = mysqli_connect("localhost", "root", "");
    
    mysqli_select_db($conn, "test");
    
    mysqli_autocommit($conn, true);
    
    
    
    mysqli_query($conn, "INSERT INTO test_table (col1) VALUES ('value1')");
    
    
    
    if (mysqli_autocommit($conn, false)) {
    
        $result = mysqli_commit($conn);
    
        if (!$result) {
    
            echo "Error: " . mysqli_error($conn);
    
        }
    
    } else {
    
        echo "Error: " . mysqli_error($conn);
    
    }
    
    


    위의 코드에서 autocommit을 false로 설정한 후 commit() 함수를 호출합니다. 이로 인해 autocommit이 false로 설정된 상태에서 commit() 함수를 호출할 수 있습니다.

    2025-04-11 23:36

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 10,958건 / 17 페이지

검색

게시물 검색