mysqli::change_user

(PHP 5, PHP 7, PHP 8)

mysqli::change_user -- mysqli_change_user — 지정된 데이터베이스 연결의 사용자를 변경합니다.


설명

객체 지향 스타일

public mysqli::change_user(string $username, string $password, ?string $database): bool

절차적 스타일

mysqli_change_user(mysqli $mysql, string $username, string $password, ?string $database): bool

지정된 데이터베이스 연결의 사용자를 변경하고 현재 데이터베이스를 설정합니다.

사용자를 성공적으로 변경하려면 유효한 usernamepassword 매개변수를 제공해야 하며 해당 사용자는 원하는 데이터베이스에 액세스할 수 있는 충분한 권한이 있어야 합니다. 어떤 이유로든 인증이 실패하면 현재 사용자 인증이 유지됩니다.


매개변수

mysql
절차적 스타일 전용: mysqli_connect() 또는 mysqli_init()에 의해 반환된 mysqli 객체
username
MySQL 사용자 이름.
password
MySQL 비밀번호입니다.
database
변경할 데이터베이스입니다.

원하는 경우 null 값이 전달되어 사용자만 변경하고 데이터베이스는 선택하지 않을 수 있습니다. 이 경우 데이터베이스를 선택하려면 mysqli_select_db() 함수를 사용하십시오.


반환 값

성공하면 true를, 실패하면 false를 반환합니다.


Examples

예제 #1 mysqli::change_user() 예제

객체 지향 스타일

                  
<?php

/* connect database test */
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Set Variable a */
$mysqli->query("SET @a:=1");

/* reset all and select a new database */
$mysqli->change_user("my_user", "my_password", "world");

if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database: %s\n", $row[0]);
    $result->close();
}

if ($result = $mysqli->query("SELECT @a")) {
    $row = $result->fetch_row();
    if ($row[0] === NULL) {
        printf("Value of variable a is NULL\n");
    }
    $result->close();
}

/* close connection */
$mysqli->close();
?>
                  
                

절차적 스타일

                  
<?php
/* connect database test */
$link = mysqli_connect("localhost", "my_user", "my_password", "test");

/* check connection */
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Set Variable a */
mysqli_query($link, "SET @a:=1");

/* reset all and select a new database */
mysqli_change_user($link, "my_user", "my_password", "world");

if ($result = mysqli_query($link, "SELECT DATABASE()")) {
    $row = mysqli_fetch_row($result);
    printf("Default database: %s\n", $row[0]);
    mysqli_free_result($result);
}

if ($result = mysqli_query($link, "SELECT @a")) {
    $row = mysqli_fetch_row($result);
    if ($row[0] === NULL) {
        printf("Value of variable a is NULL\n");
    }
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>
                  
                

위의 예는 다음을 출력합니다.

Default database: world
Value of variable a is NULL
                

노트

메모: 이 명령을 사용하면 작업이 성공적으로 완료되었는지 여부에 관계없이 항상 현재 데이터베이스 연결이 완전히 새로운 데이터베이스 연결인 것처럼 작동합니다. 이 재설정에는 활성 트랜잭션에 대한 롤백 수행, 모든 임시 테이블 닫기, 잠긴 테이블 잠금 해제가 포함됩니다.


기타