我正在做一個Android的登錄註冊的教程由tonakami電視在youtube,我遇到了一個問題,其中Android應用程序無法更新或連接到數據庫通過LOCALHOST XAMPP。一個星期,我堅持搜索所有可能的解決方案,但沒有發現。也許你們可以幫我解決我的PHP文件問題(register.php和fetchuserdata.php),我也發現我在php文件中也有這個錯誤:mysqli_connect():(HY000/1044):訪問拒絕用戶'my_user'@'localhost'到數據庫'my_db'。我授予所有權限,但仍然無法連接,我不知道問題是從PHP文件,到android studio中的服務器還是XAMPP本地主機phpmyadmin的鏈接。謝謝 !如何連接或鏈接到本地主機服務器地址在Android Studio
Screenshot of Android Studio: highlighted link
Register.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO User (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
FetchUserData.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["name"] = $name;
$user["age"] = $age;
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
,誰是調用這些PHP腳本?並從哪裏? – greenapps
你真的創建了一個名爲my_user的用戶嗎?你有一個名爲my_db的數據庫?也可以看看你周圍的用戶名引用等等。你想要這樣的勾號'不是引用'。用' – apesa
替換所有引號'你使用的是什麼模擬器?Genymotion或Android默認模擬器? –