php - 无法更改 PHP MySQLi 连接的字符集

标签 php mysql mariadb

为什么当我连接服务器上的 mysql 客户端时,我可以运行“CHARSET utf8mb4”并更改连接的字符集,但是当我尝试通过 mysqli 执行相同的语句时,会出现以下错误:

ERROR SQL error in "charset utf8mb4"; You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'charset utf8mb4' at line 1

最佳答案

据我所知,MySQL Server也没有任何charset命令。您可以使用 SET NAMES 指定字符集,这是设置几个相关变量的快捷方式(所有这些变量都可以使用 SET 命令设置)。

您可能对官方命令行客户端的命令感到困惑:

mysql> \h

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
notee     (\t) Don't write into outfile.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

由于您使用的是 PHP,而不是命令行客户端,因此它不存在。

当然,mysqli设置编码的方式是mysqli::set_charset() .

关于php - 无法更改 PHP MySQLi 连接的字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42277493/

相关文章:

mysql - rails : Creating a survey data structure with variable type answers

mysql - 保留一些特殊的主键(其中启用了auto_increment)

mysql - phpMyAdmin 在尝试编辑或删除行时给出错误 #1064

mysql - MariaDB:命令行不显示返回的行数

php - mysql 的 UNION 查询有问题

php - 变量未通过引用传递

javascript - 如何使用ajax发送图像和数据

php - 在 laravel/blade 中从数组创建逗号分隔列表?

php - PDO LIKE 语句的正确语法

mysql - having 子句会跳过计算字段的偶数值