我在 eclipse 中创建了一个 android 登录页面。并通过 wamp 服务器使用 php 代码将 mysql 数据库连接到页面。但是在运行php文件时,我收到错误
1) 警告:mysql_connect() [function.mysql-connect]:用户 'root'@'localhost' (使用密码: YES) 在第 6 行的 C:''wamp''www''log''login.php 中被拒绝访问
2) 注意:用户"root"@'localhost'(使用密码:YES)在第 8 行的 C:''wamp''www''log''login.php 中的访问被拒绝
3) 警告:mysql_select_db() 期望参数 2 是资源,布尔值在第 10 行的 C:''wamp''www''log''login.php 中给出
4)注意:未定义的索引:C:''wamp''www''log''login.php 第 12 行中的用户名
5)注意:未定义的索引:密码在 C:''wamp''www''log''login.php 第 13 行
6)未选择数据库
同样在模拟器中,我在"验证用户"后被停止。请帮助我。注意:我的mysql用户名和密码是"root"和"admin"屏幕1.java
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();
}
});
}
}
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
PHP代码
<?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";
}
?>
Access denied for user 'root'@'localhost' (using password: YES) in C:'wamp'www'log'login.php on line 6
告诉您使用错误的凭据连接到MySQL数据库和/或无法访问主机名。
-
如果您在安装了 WAMP 的桌面上,请尝试使用"root"作为用户名和空白密码 (")。
-
如果您使用的是Android设备,则必须确保它可以连接到您的数据库(如果您使用
localhost
作为主机名,则可能无法
)。
确保为数据库主机使用正确的username
和password
,稍后只需尝试此代码。这应该行得通。
如果您使用wamp
Windows,则密码将仅为""
(无密码)
<?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";
}
}
?>
对于安卓,如果你想把它连接到本地主机,
更改 Java 类上的此行代码
httppost= new HttpPost("http://10.0.2.2:80/log/login.php"); // make sure the url is correct.