在我的.html文档中,我的表单是
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
在.php文件中,我连接到我尝试使用
的数据库mysql_set_charset('utf8');
// and
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
和此:
header('Content-Type: text/html; charset=utf-8');
数据库行包含排序规则:utf8_unicode_ci
和字符集utf8
所以我的问题是,当我从html表单通过php发送代码时,我在我的数据库中看到这个:Ð〜збÐμÑ€ÐμÑ
这是.php文档代码:
<?php
// header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/html; charset=utf8');
// mysql_set_charset('utf8_unicode_ci');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$name = $_POST['name'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// mysql_set_charset('utf8');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO oglasi (Name)
VALUES ('$name')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
// mysql_set_charset('utf8');
?>
答案 0 :(得分:1)
PROCEDURAL:
mysqli_set_charset(连接,字符集);
$con=mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_set_charset($con,"utf8");
OOP:
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
exit();
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
答案 1 :(得分:0)
在执行插入查询之前,您应该运行:
$conn->query("SET NAMES 'utf8'");