[英]Character encoding issue with PHP/MySQL
I have a web application I have been developing locally on my computer, I have recently launched it on my web server with the exact same settings for MySQL, but for some reason I am encountering a character encoding issue where "smart quotes" (') are being displayed as black triangles with question marks ( ), but only on the server.我有一个在我的计算机上本地开发的 Web 应用程序,我最近在我的 Web 服务器上使用 MySQL 的完全相同的设置启动了它,但由于某种原因,我遇到了字符编码问题,其中“智能引号”(')显示为带有问号 ( ) 的黑色三角形,但仅在服务器上显示。
This is on the HTML:这是在 HTML 上:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
My local MySQL database (without character issues) is MySQL 5.7.26 with an InnoDB table with UTF8MB4
encoding and a table collation of utf8mb4-unicode-ci
我的本地 MySQL 数据库(没有字符问题)是 MySQL 5.7.26,带有一个 InnoDB 表,采用
UTF8MB4
编码,表排序规则为utf8mb4-unicode-ci
My server MySQL database (with character issues) is MySQL 5.5.5-10.3.21 with an InnoDB table with UTF8MB4
encoding and a table collation of utf8mb4-unicode-ci
我的服务器 MySQL 数据库(有字符问题)是 MySQL 5.5.5-10.3.21,带有一个带有
UTF8MB4
编码的 InnoDB 表和一个utf8mb4-unicode-ci
的表整理
Perhaps there is a PHP setting somewhere I am missing?也许我错过了某个地方的 PHP 设置?
Edit:编辑:
if I run this script on my computer is works perfectly, but if I run the same script on my server with the same data in the database, it give me the character encoding issue:如果我在我的电脑上运行这个脚本是完美的,但是如果我在我的服务器上运行相同的脚本,数据库中的数据相同,它会给我一个字符编码问题:
error_reporting(E_ALL);
include 'dbconfig.php';
$id = 19;
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$sql = $pdo->prepare("SELECT * FROM products WHERE id=:id");
$sql->execute(['id' => $id]);
var_dump ($sql->fetch());
while ($row = $sql->fetch()) {
$data[] = $row;
}
echo json_encode($data);
} catch(PDOException $e) {
//echo "Error: " . $e->getMessage();
echo "Database Error";
}
$conn = null;
(Since this clearly shows a PDO omission, I am reopening) (因为这清楚地表明 PDO 遗漏,我重新打开)
One-line change:一行修改:
$pdo = new
PDO("mysql:host=$servername;dbname=$dbname;dbname=db;charset=utf8mb4'",
$username, $password);
References:参考:
http://mysql.rjweb.org/doc.php/charcoll#php http://mysql.rjweb.org/doc.php/charcoll#php
Trouble with UTF-8 characters; UTF-8 字符问题; what I see is not what I stored (Look especially for "black diamond".)
我看到的不是我存储的(特别是寻找“黑钻石”。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.