PHP mysqli_get_charset() 函式



定義和用法

mysqli_get_charset() 函式返回一個字元集類的物件,該物件包含以下屬性:

  • charset: 字元集名稱。

  • collation: 排序規則名稱。

  • dir: 字元集的方向。

  • min_length: 最小字元長度(位元組)。

  • max_length: 最大字元長度(位元組)。

  • number: 字元集編號。

  • state: 字元集狀態。

語法

mysqli_get_charset($con)

引數

序號 引數及說明
1

con(必填)

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

返回值

mysqli_get_charset() 函式返回一個字元集類的物件。

PHP 版本

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

示例

以下示例演示了 mysqli_get_charset() 函式的使用(過程式風格):

<?php
  $db = mysqli_init();
  //Creating the connection
  mysqli_real_connect($db, "localhost","root","password","test");
  //Character set
  $res = mysqli_get_charset($db);
  print_r($res);
?>

這將產生以下結果:

stdClass Object
(
    [charset] => utf8
    [collation] => utf8_general_ci
    [dir] =>
    [min_length] => 1
    [max_length] => 3
    [number] => 33
    [state] => 1
    [comment] => UTF-8 Unicode
)

示例

在面向物件風格中,此函式的語法為 $db->get_charset(); 以下是在面向物件風格中使用此函式的示例:

<?php
   $db = mysqli_init();
   //Connecting to the database
   $db->real_connect("localhost","root","password","test");

   //Name of the character set
   $res = $db->get_charset();

   print_r($res);
?>

這將產生以下結果:

stdClass Object
(
    [charset] => utf8
    [collation] => utf8_general_ci
    [dir] =>
    [min_length] => 1
    [max_length] => 3
    [number] => 33
    [state] => 1
    [comment] => UTF-8 Unicode
)

示例

<?php
   $connection_mysql = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   var_dump(mysqli_get_charset($connection_mysql));
   mysqli_close($connection_mysql);
?>

這將產生以下結果:

object(stdClass)#2 (8) {
  ["charset"]=>
  string(4) "utf8"
  ["collation"]=>
  string(15) "utf8_general_ci"
  ["dir"]=>
  string(0) ""
  ["min_length"]=>
  int(1)
  ["max_length"]=>
  int(3)
  ["number"]=>
  int(33)
  ["state"]=>
  int(1)
  ["comment"]=>
  string(13) "UTF-8 Unicode"
}

Default character set is: utf8
php_function_reference.htm
廣告