Php 将mysql数据库与Android应用程序连接
我在用Android应用程序连接数据库时遇到问题。我正在尝试实现教程。一切似乎都很好,但我既没有成功也没有失误 有一个按钮监听器,它在单击时向PHP文件发送一个帖子并获得结果。下面是它的代码:-Php 将mysql数据库与Android应用程序连接,php,android,mysql,Php,Android,Mysql,我在用Android应用程序连接数据库时遇到问题。我正在尝试实现教程。一切似乎都很好,但我既没有成功也没有失误 有一个按钮监听器,它在单击时向PHP文件发送一个帖子并获得结果。下面是它的代码:- ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList<NameValuePair&
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/check.php", postParameters);
String res=response.toString();
Log.d("res:", res);
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(res.equals("1"))
error.setText("Correct Username or Password");
else
error.setText("Sorry!! Incorrect Username or Password");
} catch (Exception e) {
un.setText(e.toString());
}
}
});
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,un.getText().toString());
添加(新的BasicNameValuePair(“密码”,pw.getText().toString());
//字符串valid=“1”;
字符串响应=null;
试一试{
响应=CustomHttpClient.executeHttpPost(“http://10.0.2.2/check.php“,后参数);
String res=response.toString();
Log.d(“res:”,res);
//res=res.trim();
res=res.replaceAll(“\\s+”,”);
//错误.setText(res);
如果(相对等于(“1”))
错误.setText(“正确的用户名或密码”);
其他的
error.setText(“对不起!!用户名或密码不正确”);
}捕获(例外e){
un.setText(例如toString());
}
}
});
以下是http post方法:-
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
Log.d("postMethodReturn", result);
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
publicstaticstringexecutehttpost(stringurl,arraylistpostparameters)引发异常{
BufferedReader in=null;
试一试{
HttpClient=getHttpClient();
HttpPost请求=新的HttpPost(url);
UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(后参数);
请求。setEntity(formEntity);
HttpResponse response=client.execute(请求);
in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
而((line=in.readLine())!=null){
sb.追加(行+NL);
}
in.close();
字符串结果=sb.toString();
Log.d(“方法后返回”,结果);
返回结果;
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
PHP代码如下所示:-
<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db
$user = "xyz";
$pswd = "xyz";
$db = "mydb";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db);
//run the query to search for the username and password the match
$query = "SELECT * FROM mytable WHERE user = '$un' AND 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
?>
问题在于PHP代码中的-->
。将其更改为==
或
,一切正常 你能告诉我打印的结果是什么吗?它总是以0(不正确的登录响应)的形式给出输出,不幸的是在请求的地方没有日志。这表明prblm是php函数,不在android端…所以请先检查php连接OK谢谢。关于如何检查有什么建议吗?我在/var/www下有一个php文件,我可以从同一目录下的浏览器中看到index.html文件。。。享受您的DB pwn3d。另外,php语言规范中哪里定义了-->
操作符?