doujia2386
2016-01-29 17:24
浏览 606

如何在Android Studio中连接或链接到localhost服务器地址

I am following a tutorial of making a login register for android by tonakami TV in youtube, I encountered a problem where the android app can't update or connect to the database via LOCALHOST XAMPP. For one week I'm stuck searching all possible solution but found none. Perhaps you guys can help me with the problem of my PHP files(register.php and fetchuserdata.php), I also found out that I also have this error in the php files: mysqli_connect(): (HY000/1044): Access denied for user 'my_user'@'localhost' to database 'my_db'. I grant all privileges but still can't connect and I don't know if the problem is from the PHP files, the link to the server in android studio or of XAMPP localhost phpmyadmin. Thanks !

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);
?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbei7035 2016-01-29 17:29
    已采纳

    Instead of localhost use this: http://10.0.2.2 Reference

    打赏 评论

相关推荐 更多相似问题