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

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

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?