%20%E5%82%B3%E9%80%81%E5%88%B0%20PHP%2FMySQL%20%E4%B8%AD%E7%9A%84%E8%B3%87%E6%96%99%E5%BA%AB%E8%A1%A8%E6%99%82%E5%87%BA%E7%8F%BE%E5%95%8F%E9%A1%8C.png)
我在使 mydatabase 表顯示拉丁字元時遇到問題。 (ã、õ、á、é、í、...)。我正在使用帶有 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
。