將拉丁重音字元 (ã,õ,á,é,í,…) 傳送到 PHP/MySQL 中的資料庫表時出現問題

將拉丁重音字元 (ã,õ,á,é,í,…) 傳送到 PHP/MySQL 中的資料庫表時出現問題

我在使 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

相關內容