PHP mysqli_change_user() 函式



定義和用法

mysqli_change_user() 函式接受一個連線物件、使用者名稱、密碼和資料庫名稱作為引數,將給定連線物件中的使用者和資料庫更改為指定的使用者和資料庫。

語法

mysqli_change_user($con, $user, $password, $database);

引數

序號 引數及說明
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件。

2

user(可選)

這是您需要更改到的 MySQL 使用者的名稱。

3

password(可選)

這是指定 MySQL 使用者的密碼

3

database(可選)

這表示您需要更改到的資料庫的名稱。如果將 NULL 作為值傳遞給此引數,則此函式僅更改使用者而不選擇資料庫。

返回值

PHP mysqli_change_user() 函式返回一個布林值,如果資料庫成功更改則為 true,否則為 false

PHP 版本

此函式首次在 PHP 5 版本中引入,並在所有後續版本中均有效。

示例

以下示例演示了 mysqli_change_user() 函式(在過程式風格中)的使用 -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   $res = mysqli_change_user($con, "Tutorialspoint", "abc123", "mydb");

   if($res){
      print("User changed successfully");
   }else{
      print("Sorry Couldn't change the user");
   }

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

User changed successfully

示例

在面向物件風格中,此函式的語法為 $con->change_user(); 以下是此函式在面向物件風格中的示例 $minus;

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = new mysqli($host, $username, $passwd, $dbname);

   $res = $con->change_user("Tutorialspoint", "abc123", "mydb");

   if($res){
      print("User changed successfully");
   }else{
      print("Sorry couldn't change the user");
   }

   //Closing the connection
   $res = $con -> close();

?>

這將產生以下結果 -

User changed successfully

示例

您可以驗證更改後的資料庫名稱,如下所示 -

//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");

//Changing the database
$res = mysqli_change_user($con, "Tutorialspoint", "abc123", "mydb");

$list = mysqli_query($con, "SELECT DATABASE()");

if($list) {
    $row = mysqli_fetch_row($list);
    print("Current Database: ". $row[0]);
}

//Closing the connection
mysqli_close($con);
?>

這將產生以下結果 -

Current Database: mydb

示例

<?php
   $connection = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }   
   mysqli_change_user($connection, "myuser", "abc123", "sampledb"); 
   mysqli_close($connection);
?>
php_function_reference.htm
廣告