• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Connecting to a mysql Database

hey everyone,

Having a few problems with my app. I want to make it so I connect to a mysql database to allow people to login. However, when I click the go button, nothing is happening...

PHP script:
Code:
<?php
$hostname_localhost ="localhost";
$database_localhost ="profiled_Android";
$username_localhost ="profiled_Moodle";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "SELECT * from moodle_user = '".$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"; 
}
?>

Activity_main.xml
Code:
<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: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=".MainActivity" >

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login"
        android:layout_alignLeft="@+id/username"
        android:layout_marginBottom="49dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:adjustViewBounds="true"
        android:contentDescription="@string/desc_image"
        android:src="@drawable/logo" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="49dp"
        android:text="Button" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:ems="10"
        android:hint="Username" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/greetingtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="26dp"
        android:layout_toLeftOf="@+id/login"
        android:text="TextView" />

</RelativeLayout>

MainActivity.java:
Code:
package com.profiledt.helloworld;

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.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	Button login;
	EditText username, password;
	TextView status;
	HttpPost httppost;
	StringBuffer buffer;
	HttpResponse response;
	HttpClient httpclient;
	List<NameValuePair> nameValuePair;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setup();
	}

	private void setup() {
		// TODO Auto-generated method stub
		
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		login = (Button) findViewById(R.id.login);
		status = (TextView) findViewById(R.id.greetingtext);
		login.setOnClickListener(this);	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId()) {
		case R.id.login:
			
			login();
			
			break;
		}
		}
	
	private void login() {
		// TODO Auto-generated method stub
		try {
			httpclient= new DefaultHttpClient();
			httppost= new HttpPost("http://profiledt.co.uk/android/index.php"); 
		//  Adding Data:
			nameValuePair = new ArrayList<NameValuePair>(1);
		//  Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
			nameValuePair.add(new BasicNameValuePair("username",username.getText().toString().trim()));
			nameValuePair.add(new BasicNameValuePair("password",password.getText().toString().trim()));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		// Execute HTTP Post Request
			response = httpclient.execute(httppost);
			
			
			
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			final String response = httpclient.execute(httppost, responseHandler);
			status.setText(""+response);
			if(response.equalsIgnoreCase("No user found")) {
				
				startActivity(new Intent(MainActivity.this, UserPage.class));
			}
			
			
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
my database:
cieF4Pe.png


Thanks guys :)
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones