无法使用php和mysql登录android应用程序
我在eclipse中创建了一个android登录页面,并通过wamp服务器使用php代码将mysql数据库连接到该页面。但是在运行php文件时,我得到了如下错误 1) 警告:mysql\u connect()[function.mysql connect]:第6行的C:\wamp\www\log\login.php中的用户“root”@“localhost”(使用密码:YES)的访问被拒绝 2) 注意:第8行C:\wamp\www\log\login.php中的用户“root”@“localhost”(使用密码:YES)的访问被拒绝 3) 警告:mysql\u select\u db()希望参数2是资源,布尔值在第10行的C:\wamp\www\log\login.php中给出 4) 注意:第12行C:\wamp\www\log\login.php中的未定义索引:username 5) 注意:第13行C:\wamp\www\log\login.php中的未定义索引:密码 6) 未选择任何数据库 而且在模拟器中,我在“验证用户”后被停止。请帮助我。 注意:我的mysql用户名和密码是“root”和“admin” Screen1.java无法使用php和mysql登录android应用程序,php,android,mysql,Php,Android,Mysql,我在eclipse中创建了一个android登录页面,并通过wamp服务器使用php代码将mysql数据库连接到该页面。但是在运行php文件时,我得到了如下错误 1) 警告:mysql\u connect()[function.mysql connect]:第6行的C:\wamp\www\log\login.php中的用户“root”@“localhost”(使用密码:YES)的访问被拒绝 2) 注意:第8行C:\wamp\www\log\login.php中的用户“root”@“localho
package com.example.library;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Screen1 extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen1);
b = (Button)findViewById(R.id.loginBtn);
et = (EditText)findViewById(R.id.usernameET);
pass= (EditText)findViewById(R.id.passwordET);
// tv = (TextView)findViewById(R.id.);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(Screen1.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://localhost/log/login.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Screen1.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(Screen1.this, Screen2.class));
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
Screen1.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Screen1.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
package com.example.library;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.BasicResponseHandler;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类Screen1扩展活动{
按钮b;
编辑文本,通过;
文本视图电视;
HttpPost-HttpPost;
字符串缓冲区;
HttpResponse响应;
HttpClient-HttpClient;
列出nameValuePairs;
ProgressDialog=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_屏幕1);
b=(按钮)findViewById(R.id.loginBtn);
et=(EditText)findViewById(R.id.usernameET);
pass=(EditText)findViewById(R.id.passwordET);
//tv=(文本视图)findViewById(R.id.);
b、 setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog=ProgressDialog.show(屏幕1.this,“,
“正在验证用户…”,true);
新线程(newrunnable()){
公开募捐{
登录();
}
}).start();
}
});
}
无效登录(){
试试{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://localhost/log/login.php“”;//请确保url正确无误。
//添加您的数据
nameValuePairs=新的ArrayList(2);
//发布时始终使用相同的变量名,即android端变量名和php端变量名应相似,
添加(新的BasicNameValuePair(“username”,et.getText().toString().trim());//$Edittext\u value=$\u POST['Edittext\u value'];
添加(新的BasicNameValuePair(“密码”,pass.getText().toString().trim());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
response=httpclient.execute(httppost);
//由来自coderzheaven的James编辑。从这里。。。。
ResponseHandler ResponseHandler=新BasicResponseHandler();
最终字符串响应=httpclient.execute(httppost,responseHandler);
System.out.println(“响应:+Response”);
runOnUiThread(新的Runnable(){
公开募捐{
setText(“来自PHP的响应:+Response”);
dialog.dismise();
}
});
if(response.equalsIgnoreCase(“用户找到”)){
runOnUiThread(新的Runnable(){
公开募捐{
Toast.makeText(屏幕1.this,“登录成功”,Toast.LENGTH_SHORT.show();
}
});
startActivity(新意图(屏幕1.this,屏幕2.class));
}否则{
showarert();
}
}捕获(例外e){
dialog.dismise();
System.out.println(“异常:+e.getMessage());
}
}
公开作废showAlert(){
Screen1.this.rununuithread(新的Runnable(){
公开募捐{
AlertDialog.Builder=新建AlertDialog.Builder(屏幕1.this);
setTitle(“登录错误”);
builder.setMessage(“未找到用户”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
}
}
activity_screen1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/index"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.library.Screen1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#ffffffff"
android:text="Please signin"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="75dp"
android:textColor="#ffffffff"
android:layout_marginLeft="10dp"
android:text="Rollno:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/usernameET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="35dp"
android:textColor="#ffffffff"
android:layout_toRightOf="@+id/textView2"
android:hint="eg:201103005" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="40dp"
android:text="Password:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/passwordET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/usernameET"
android:layout_alignRight="@+id/usernameET"
android:hint="*******"
android:textColor="#ffffffff"
android:inputType="textPassword" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/passwordET"
android:layout_centerHorizontal="true"
android:layout_marginTop="95dp"
android:background="@drawable/sign_in_button"
android:onClick="jumpscreen2"/>
</RelativeLayout
第6行C:\wamp\www\log\login.php中的用户“root”@“localhost”(使用密码:YES)的访问被拒绝
告诉您使用错误的凭据连接到MySQL数据库和/或主机名不正确
<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="admin";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error());
mysql_select_db($database_localhost, $localhost);
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "No Such User Found";
}
else {
echo "User Found";
}
?>
<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error());
mysql_select_db($database_localhost, $localhost);
$username = $_POST['username'];
$password = $_POST['password'];
$query = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$result = @mysql_query($query);
if(!$result){
die('ERROR: '.mysql_error());
}
else{
if( mysql_num_rows($result) > 0) {
echo "User found \n";
}
else{
echo "not found";
}
}
?>