我在使数据库表显示拉丁字符时遇到问题。(ã、õ、á、é、í、…)。我正在使用带有 Apache 2.4.9 和 MySQL 5.6.17 的 WAMP 服务器。
我有一个简单的 HTML 表单,还有一个 PHP 代码,用于运行查询并创建帐户。以下是 PHP 代码:
include('config.php'); //My database data for connection
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$query = "INSERT INTO account SET username = '".$username."', password = PASSWORD('".$password."'), email = '".$email."'";
$execute_query = mysqli_query($connection, $query);
if($execute_query) {
//Creates the account
}
else {
//If an error occures
}
}
现在例如,我使用以下命令创建一个帐户:
Username: Luís
Password: 1234
E-mail: [email protected]
当我检查表格时,它显示已创建的帐户,但用户名显示路易斯代替路易斯。
我尝试过的事情:
Creating a DataBase and Table with:
utf8_general_ci
utf8_unicode_ci
utf8mb4_general_ci
utf8mb4_unicode_ci
latin1_swedish_ci
它们都不起作用,它总是表明A代替我。
答案1
我已经解决了查询“SET NAMES utf8”的问题。
include('config.php'); //My Database data for connection.
//Connection set with $connection = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db)
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$connection->query("SET NAMES utf8"); //FIXED (Gets $connection from config.php and runs the query "SET NAMES utf8")
$query = "INSERT INTO account SET username = '".$username."', password = PASSWORD('".$password."'), email = '".$email."'";
$execute_query = mysqli_query($connection, $query);
if($execute_query) {
//Creates the account
}
else {
//If an error occures
}
}
谢谢@Giacomo1968 试图帮助我回答,毕竟我的数据库表没有问题。
我的数据库和表是用CHARSET=utf8
和设置的COLLATE=utf8_unicode_ci
。