如何在 php7 的 mysqli 查询中使用汉字作为关联键?
How to use chinese character as associate key in php7's mysqli query?
对于我的 mysql 数据库。
mysql> show variables like 'character%';
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | F:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
mysql> show variables like "collation%";
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | gb2312_chinese_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
我想 select 在 php7 中与 mysqli 分享一些信息。
名为 getdata1.php
的文件编码为 utf-8
。
My getdata1.php file.
<?php
header("Content-type: text/html; charset=utf-8");
$mysqli = new mysqli("localhost", "root", "xxxxxx", "student");
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
$row = $result->fetch_assoc();
print_r($row);
$result->close();
$mysqli->close();
?>
我得到了以下输出。
Array ( [ID] => 1 [学号] => 11410104 [姓名] => 陈XX)
据我所知,汉字可用于 php 的关联数组的关联键和值。
现在我想用 $row["姓名"]
.
获取数据
以下文件 getdata2.php
也在 utf-8
中编码,我一无所获。
My getdata2.php file.
<?php
header("Content-type: text/html; charset=utf-8");
$mysqli = new mysqli("localhost", "root", "xxxxxx", "student");
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
while ($row = $result->fetch_row()) {
printf ("%s \n", $row["姓名"]);
}
$result->close();
$mysqli->close();
?>
检查 apache 中的日志文件。
Notice: Undefined index: \xe5\xa7\x93\xe5\x90\x8d in F:\wamp\apache2\htdocs\getdata2.php on line 7
[Sun Aug 12 20:25:23.217182 2018] [:error] [pid 12204:tid 1264] [client 127.0.0.1:54023]
\xe5\xa7\x93\xe5\x90\x8d
字符串是 姓名
的 utf-8 编码。
python3
>>> b"\xe5\xa7\x93\xe5\x90\x8d".decode("utf-8")
'姓名'
如何在php7的mysqli查询中使用汉字作为关联键?
fetch_row()
生成索引数组。
您在需要数字时提供汉字,请将您的功能更改为 fetch_assoc()
。
为了让你以后有能力实现这一点,你应该在卡住的时候写var_export($result->fetch_row());
,这样你就可以看到1中实际上是什么了-dim 数组并确定您的元素访问尝试失败的原因。
作为一个良好的通用做法,您应该将查询中的 *
替换为您打算使用的单独列——这样结果集就不会包含任何不必要的膨胀。
对于我的 mysql 数据库。
mysql> show variables like 'character%';
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | F:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
mysql> show variables like "collation%";
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | gb2312_chinese_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
我想 select 在 php7 中与 mysqli 分享一些信息。
名为 getdata1.php
的文件编码为 utf-8
。
My getdata1.php file.
<?php
header("Content-type: text/html; charset=utf-8");
$mysqli = new mysqli("localhost", "root", "xxxxxx", "student");
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
$row = $result->fetch_assoc();
print_r($row);
$result->close();
$mysqli->close();
?>
我得到了以下输出。
Array ( [ID] => 1 [学号] => 11410104 [姓名] => 陈XX)
据我所知,汉字可用于 php 的关联数组的关联键和值。
现在我想用 $row["姓名"]
.
获取数据
以下文件 getdata2.php
也在 utf-8
中编码,我一无所获。
My getdata2.php file.
<?php
header("Content-type: text/html; charset=utf-8");
$mysqli = new mysqli("localhost", "root", "xxxxxx", "student");
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
while ($row = $result->fetch_row()) {
printf ("%s \n", $row["姓名"]);
}
$result->close();
$mysqli->close();
?>
检查 apache 中的日志文件。
Notice: Undefined index: \xe5\xa7\x93\xe5\x90\x8d in F:\wamp\apache2\htdocs\getdata2.php on line 7
[Sun Aug 12 20:25:23.217182 2018] [:error] [pid 12204:tid 1264] [client 127.0.0.1:54023]
\xe5\xa7\x93\xe5\x90\x8d
字符串是 姓名
的 utf-8 编码。
python3
>>> b"\xe5\xa7\x93\xe5\x90\x8d".decode("utf-8")
'姓名'
如何在php7的mysqli查询中使用汉字作为关联键?
fetch_row()
生成索引数组。
您在需要数字时提供汉字,请将您的功能更改为 fetch_assoc()
。
为了让你以后有能力实现这一点,你应该在卡住的时候写var_export($result->fetch_row());
,这样你就可以看到1中实际上是什么了-dim 数组并确定您的元素访问尝试失败的原因。
作为一个良好的通用做法,您应该将查询中的 *
替换为您打算使用的单独列——这样结果集就不会包含任何不必要的膨胀。