我无法将 UTF-8 字符串添加到 MySQL 数据库中。代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$temp='papà';
mysql_query("SET NAMES utf8");
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
mysql_query("INSERT INTO comment VALUE ('$temp')") or die(mysql_error());
?>
</body>
</html>
DB 中的变量是 CHAR(120),并且使用非 utf8 字符串可以正常工作。 该错误是一般性的“您的 SQL 语法 ecc 中有错误。”
预先感谢您的帮助。
最佳答案
使用以下代码:
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$temp='papà';
mysqli_query("INSERT INTO comment VALUE ('$temp')") or die(mysqli_error());
?>
</body>
</html>
停止使用mysql_*
并开始使用mysqli_*
或使用PDO
关于php - 无法将 UTF-8 字符串添加到 MySQL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309062/