Hi I am new to android and I am developing an app which has a login screen.
I have tried many tutorials but I am not sure how to connect the mysql database to my android app. In all the tutorials they have used php to connect the android app to the database.
What I want to know is that where exactly do I need to place the php file and where and how I need to create the php file.
I am creating the database using mysql workbench.
I am using eclipse 4.2 to write the java code.
Hi i am using the following code to check whether the user name i entered is correct or not along with the php code but the problem is that it always gives incorrect username.
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://192.168.3.27/abc/check.php", postParameters); //Enetr Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
error.setText(res);
if(res.equals("1")){
Intent myIntent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(myIntent);
}
else
error.setText("Sorry!! Incorrect Username or Password");
This is my php code.
<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db
$user = ‘root’;
$pswd = ‘root’;
$db = ‘mylogin’;
$conn = mysql_connect(‘localhost’, $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = “SELECT * FROM userpass WHERE login_id = ‘$un’ AND login_pass = ‘$pw’”;
$result = mysql_query($query) or die(“Unable to verify user because : ” . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
echo 1; // for correct login response
else
echo 0; // for incorrect login response
?>
You need to place PHP file in server. (You can use localhost i.e., your machine but for devices outside you need to keep it on a webserver which you need to purchase a domain and hosting services.)
You can create a PHP file in you machine itself. Its as simple as creating a text file but with an extension of
.php. Using a wamp on windows (LAMP for linux)you can test it. It has a MySQL in it. LAMP and WAMP will have apache server by default.Soon after you are finished with writing you php code and testing you can transfer the files through FTP into your webserver. Now to configure the MySQL database you can actually use a control panel at the webserver.
You need to use URL for android application to link the PHP files in turn these PHP files interact with MysQL. for a login lets think like you have created a php file as login.php. On your localhost you can refer it to as
http://localhost/myapp/login.phpIf you need to get it on a webserver which you purchase then you URL will havehttp://www.yourwebsite.com/myapp/login.php. note that myapp is just a folder where you have uploaded your php files.Now its just a way by which you can actually have a PHP and MySQL for you android application. I think that tutorials have taught you about using php and mysql connections. For Data exchange you need to know about XML or JSON I think tutorials followed had given you an introduction about it.
You even have a plugin for eclipse to work with php. Just get a help over internet on how to install it. This video might help you.