本质上,我的JSON为包含特殊字符的字段打印NULL。
我已经阅读过该主题,并按照重复的问题以及其他主题,特别强调了一个帖子,我需要执行以下操作:
If you're using mysqli, you can call set_charset():
$mysqli->set_charset('utf8mb4'); // object oriented style
mysqli_set_charset($link, 'utf8mb4'); // procedural style
If you're stuck with plain mysql but happen to be running PHP ≥ 5.2.3, you can call mysql_set_charset.
If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded: SET NAMES 'utf8mb4'.
我已按照openConnection()
函数中的说明完成此操作,如下面的连接脚本中所示:
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
class MySQLDao{
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;
public function __construct(){
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}
public function openConnection()
{
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
mysqli_set_charset($this->conn, "utf8mb4");
}
我在phpMyAdmin中浏览了我的数据库,并将所有归类转换为ut8mb4_bin。我按照建议添加了mysql_set_chars
行,并且我还通过这种类型的命令将所有表格更改为utf8mb4 ALTER TABLE teams CHANGE name name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
这些是来自phpMyAdmin的数据库设置:
我的网络服务器详情是 阿帕奇 数据库客户端版本:libmysql - 5.0.77 PHP扩展:mysqli文档 PHP版本:5.3.3
然而,这一切都没有解决我的问题。
我的character_set_server显示为latin1,这可能是问题吗?
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | latin1_swedish_ci |
+--------------------------+--------------------+
我不知道还有什么可尝试的。