Php/func mysqli set charset

来自菜鸟教程
跳转至:导航、​搜索

PHP mysqli set_charset()函数

MySQL PHP MySQLi参考

示例-面向对象的样式

更改默认的客户端字符集:

<?php

$mysqli = new mysqli("localhost","my_user","my_password","my_db");


if ($mysqli -> connect_errno) {

  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  
  exit();

  }

echo "Initial character set is: " . $mysqli 
  -> character_set_name();

// Change character set to utf8
$mysqli -> set_charset("utf8");


  echo "Current character set is: " . $mysqli -> character_set_name();

$mysqli -> close();

?>



在底部查看程序样式的示例。

定义和用法

set_charset()/ mysqli_set_charset()函数指定在与数据库服务器之间发送数据时使用的默认字符集。

注意: 为了使此功能在Windows平台上运行,您需要MySQL客户端库4.1.11或更高版本(对于MySQL 5.0,则需要5.0.6或更高版本)。

句法

面向对象的样式:

$mysqli -> 
  set_charset(charset)

程序风格:

mysqli_set_charset(connection, charset)

参数值

参数 描述
connection 需要。指定要使用的MySQL连接
charset 需要。指定默认字符集

技术细节

返回值: 成功则为真。失败时为假
PHP版本: 5.0.5+

示例-程序风格

更改默认的客户端字符集:

<?php

$con=mysqli_connect("localhost","my_user","my_password","my_db");


// Check connection

 if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  
  exit;

  }

echo "Initial character set is: " . mysqli_character_set_name($con);

// Change character set to utf8

 mysqli_set_charset($con,"utf8");

echo "Current character set 
  is: " . mysqli_character_set_name($con);

mysqli_close($con);

?>



MySQL PHP MySQLi参考