doujia2386 2016-01-29 17:24
浏览 762
已采纳

如何在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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!