dongmu3187 2018-08-03 14:54 采纳率: 0%
浏览 100
已采纳

如何从结果集中获取字符集?

mysqli_result::fetch_field() returns a type property for each column, which is an integer value.

The integer value is the same for VARCHAR and VARBINARY (0xFD) columns and also for CHAR and BINARY (0xFE) columns. Those column types can be detected with MYSQLI_TYPE_STRING and MYSQLI_TYPE_VAR_STRING constants.

To know if a string column is BINARY (they have a collation called binary), or to know if columns need to be converted to another character set, the character set name is really needed.

But... mysqli_result::fetch_field() has a charsetnr property which again returns an integer for the character set. Only this time there seems to be no way of knowing the character set name, let alone the collation?

So how can one get the character set names from mysqli_result ?

  • 写回答

3条回答 默认 最新

  • doujia1988 2018-08-03 18:22
    关注

    TL;DR charsetnr is the ID of the collation as listed by SHOW COLLATION.

    I couldn't help noticing that even for numeric columns charsetnr was set to 63. This pointed me towards the manual that says:

    To distinguish between binary and nonbinary data for string data types, check whether the charsetnr value is 63. If so, the character set is binary, which indicates binary rather than nonbinary data. This enables you to distinguish BINARY from CHAR, VARBINARY from VARCHAR, and the BLOB types from the TEXT types.

    Retrieve more information about the collation and character set via:

    SELECT `COLLATION_NAME`
         , `CHARACTER_SET_NAME`
         , `IS_DEFAULT`
         , `IS_COMPILED`
         , `SORTLEN`
    FROM `INFORMATION_SCHEMA`.`COLLATIONS`
    WHERE `ID` = ?;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿