我正在从 MySQL 数据库中获取图像文件路径,一切正常,直到路径名称包含一些与其关联的 unicode 字符。例如,我使用此代码来获取图像文件路径;
$sql = "SELECT images FROM agents";
$basePath = "http://localhost/img/";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
$img = $row['images'];
$actualPath = $basePath . $img;
echo $actualPath;
}
对于英文字母图片,没关系,结果类似于 http://localhost/img/selena.jpg
但如果使用 unicode 字符,就会像 http://localhost/img/11_??.jpg
而真正的文件路径是 http://localhost/img/11_副本.jpg
如何使 副本
字符从 MySQL 中提取后保留下来,而不是仅仅成为 ??
符号?
编辑:我使用了准备好的语句方法,而不是基本的 mysqli_query()
命令。
最佳答案
在查询之前使用 SET NAMES UTF8
来处理 Unicode 字符。
mysqli_query($con, 'SET NAMES UTF8');
$sql = "SELECT images FROM agents";
$basePath = "http://localhost/img/";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
$img = $row['images'];
$actualPath = $basePath . $img;
echo $actualPath;
}
关于php - 正确从 MySQL 中提取 Unicode 字符 [PHP MYSQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296536/