How to connect or link to localhost server address in Android Studio
Instead of localhost use this: http://10.0.2.2
Reference
M. M. Human
Updated on June 16, 2022Comments
-
M. M. Human almost 2 years
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); ?>