使用MySQL数据库的Andr​​oid登录活动 [英] Android login activity using mysql database

查看:210
本文介绍了使用MySQL数据库的Andr​​oid登录活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Andr​​oid和我开发一个应用程序,它有一个登录界面。

我试过很多教程,但我不知道怎么mysql数据库连接到我的Andr​​oid应用程序。在所有的教程,他们已经使用PHP来的Andr​​oid应用程序连接到数据库。

我想知道的是,在什么地方,我需要把php文件在哪里,我需要如何创建PHP文件。

我使用MySQL的工作台创建数据库。

我使用的Eclipse 4.2编写Java code。

我现在用的是下面的code,以检查是否我输入的用户名是否正确或不随PHP code,但问题是,它总是会产生不正确的用户名。

 字符串响应= NULL;
尝试 {
响应= CustomHttpClient.executeHttpPost(http://192.168.3.27/abc/check.php,postParameters); // Enetr您的远程PHP,ASP,Servlet的文件链接
字符串解析度= response.toString();
//解析度= res.trim();
RES = res.replaceAll(\\ S +,);
//error.setText(res);
error.setText(RES);
如果(res.equals(1)){
    意图myIntent =新的意图(LoginActivity.this,HomeActivity.class);
startActivity(myIntent);

}
其他
error.setText(对不起!不正确的用户名或密码);
 

这是我的PHP code。

 < PHP
$ UN = $ _ POST [用户名];
$ PW = $ _ POST ['密码'];
//连接到数据库
$ USER ='根';
$ PSWD ='根';
$ DB ='mylogin';
$康恩=的mysql_connect('本地主机',$用户,$ PSWD);
mysql_select_db($数据库,$康恩);
//运行查询要搜索的用户名和密码匹配
$查询=SELECT * FROM userpass的WHERE login_id ='$非'和login_pass ='​​$私服';
$结果= mysql_query($查询)或死亡(无法验证用户,因为:。mysql_error());
//这是实际验证情况
如果(mysql_num_rows($结果)大于0)

回声1; //正确登录响应
其他
呼应0; //为不正确的登录响应
?>
 

解决方案
  

在什么地方做我需要把php文件

您需要把PHP文件服务器。 (外你需要保持它,你需要购买一个域名和托管服务的网络服务器,您可以使用localhost也就是说,你的机器,但对设备。)

  

在这里,我需要如何创建PHP文件。

您可以创建一个在你机器本身的PHP文件。其作为简单的创建一个文本文件,但与的.php 的延伸。在Windows上使用瓦帕(LAMP对于Linux),你可以对其进行测试。它在它的MySQL。 LAMP和WAMP将默认的Apache服务器。

你完成你写的PHP code和测试就可以通过FTP传输文件到您的网络服务器后不久。现在来配置你其实可以使用控制面板网络服务器的MySQL数据库。

您需要使用的URL为Android应用程序链接的PHP文件中把这些PHP文件与MySQL进行交互。对登录,您觉得像您创建一个PHP文件的login.php。如果你需要得到它,你购买的,那么你的URL将有一个网络服务器//localhost/myapp/login.php :在你的本地主机可以将其称之为 HTTP http://www.yourwebsite.com/myapp/login.php 。注意,MyApp的仅仅是一个地方,你都上传你的PHP文件的文件夹。

现在它只是一种方式,通过它可以实际上有一个PHP和MySQL为你的Andr​​oid应用程序。我认为,教程教您如何使用PHP和MySQL连接。对于数据交换,你需要了解XML或JSON我觉得教程随后给了你关于它的介绍。

您甚至有一个Eclipse插件,支持PHP。刚刚得到帮助了互联网上如何安装它。 的视频可能会帮助你。

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
?>

解决方案

where exactly do i need to place the php file

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.)

where and how i need to create the php file.

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.php If you need to get it on a webserver which you purchase then you URL will have http://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.

这篇关于使用MySQL数据库的Andr​​oid登录活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆