English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_connect_error() 함수는 마지막 연결 오류의 문자열 설명을 반환합니다
MySQL 서버에 연결 시도 중에 발생하면,mysqli_connect_error()함수는 발생한 오류 설명을 반환합니다(최근 연결 호출 중에).
mysqli_connect_error()
이 메서드는 어떤 매개변수도 받지 않습니다.
실패할 경우, PHP mysqli_connect_error() 함수는 상태 문자열 값을 반환하며, 이 문자열 값은 마지막 연결 호출에서 발생한 오류 설명을 나타냅니다. 연결이 성공하면 이 함수는 반환Null.
이 함수는 PHP 버전5에서 도입되었으며 모든 높은 버전에서 사용할 수 있습니다.
다음 예제는mysqli_connect_error()함수의 사용법(과정 지향 스타일)-
<?php //연결 수립 $con = @mysqli_connect("localhost", "root", "wrong_password", "mydb"); //연결 오류 $error = mysqli_connect_error($con); print("Error: ".$error); ?>
출력 결과
Error: Access denied for user 'root'@'localhost' (using password: YES)
객체 지향 스타일에서 이 함수의 문법은 다음과 같습니다$con-> connect_error아래는 이 함수의 객체 지향 스타일 예제입니다-
<?php //연결 수립 $con = @new mysqli("localhost", "root", "wrong_password", "mydb"); //연결 오류 $error = $con->connect_error; print("Error: ".$error); ?>
출력 결과
Error: Access denied for user 'root'@'localhost' (using password: YES)
다음 예제는 성공적으로 연결된 후에mysqli_connect_error()함수의 동작-
<?php //연결 수립 $con = @mysqli_connect("localhost", "root", "password", "mydb"); //연결 오류 $error = mysqli_connect_error(); if (!$con) { print("연결 실패: ".$error); } print("연결이 성공적으로 수립되었습니다"); } ?>
출력 결과
연결이 성공적으로 수립되었습니다
마지막 연결 오류의 오류 설명을 반환합니다:
<?php $connection = @mysqli_connect("localhost", "root", "wrong_pass", "wrong_db"); if (!$connection) { die("연결 오류: ". mysqli_connect_error()); } ?>테스트 봐‹/›
출력 결과
연결 오류: Access denied for user 'root'@'localhost' (using password: YES)