php - 使用 JSON 和一些特殊字符时最好的排序规则是什么

标签 php mysql json collation

我正在尝试使用 php 和 JSON 从 mysql 数据库获取数据,但由于我选择的排序规则而无法正常工作。 我使用这些字符:®、é、è、™、É 我在 mysql 中的默认排序规则是:latin1_swedish_ci,我的服务器默认排序规则是:utf8mb4_unicode_ci。 我应该删除那些特殊字符还是什么? 这是 php 代码:

<?php 
require "conn.php";
$sql = "select * from produit";
$result = mysqli_query($conn, $sql); // result contient tous les produits 
$response = array(); // on déclare un array
// pour chaque ligne de la table 
while ($row = mysqli_fetch_array($result)) { 
array_push($response, array("id_produit"=>$row[0] , "nom"=>$row[1], "catégorie"=>$row[2], "description"=>$row[3], "type"=>$row[4], "prix"=>$row[5], "qte_stock"=>$row[6]));
}
echo json_encode(array("server_response"=> $response));
mysqli_close($conn);

 ?>

显示创建表产品:

CREATE TABLE `produit` (
 `id_produit` int(11) NOT NULL AUTO_INCREMENT,
 `nom` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
 `catégorie` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
 `description` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
 `type` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
 `prix` int(11) DEFAULT NULL,
 `qte_stock` int(11) DEFAULT NULL,
 PRIMARY KEY (`id_produit`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci

最佳答案

对于所有想知道如何使用法语字符和 JSON 解决问题的人,相信我,这些天我已经尝试了所有方法,唯一有效的是:

echo json_encode(array("server_response"=>$response), JSON_UNESCAPED_UNICODE);

这里是更多信息的链接http://php.net/manual/fr/function.json-encode.php感谢上帝,我有一秒钟失去了希望

关于php - 使用 JSON 和一些特殊字符时最好的排序规则是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046118/

相关文章:

python - 在Python中,有没有办法在使用__slots__的对象上使用json.dumps?

python - JMESPath 日期过滤

php - 如何将 php 中的值附加到我的 JS 文件中?

基于MySQL查询输出的PHP决策

javascript - 根据所选单选按钮显示详细信息

php - 从引荐来源创建 session

mysql - 多个网站和发票号码 - 数据库设计

Ubuntu 18 上的 PHP oci8 安装失败

php - 避免\r\n\on mysql 导入

php - Laravel PHP - 将时间戳添加到 Artisan 控制台输出