[TUTORIAL] Connecting to MySQL database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Re: [TUTORIAL] Connecting to MySQL database

Postby thanujalakshmi » Thu Sep 29, 2011 8:35 am

Where should I place .php file?
Once Poster
Once Poster
Posts: 1
Joined: Thu Sep 29, 2011 8:32 am


Re: [TUTORIAL] Connecting to MySQL database

Postby luxi99 » Fri Oct 07, 2011 10:50 pm


First I have to say: Thank you, for the first tutorial about Android + Mysql, wich I understand.
Good job!

I have a problem.
When I run an app I got on screen just url to file where is connection to mysql.
And I don't get data from database :(

In my database I have birthyear after 1970, so that isn't a problem.

What can be a problem?
Help me, please.
Once Poster
Once Poster
Posts: 1
Joined: Fri Oct 07, 2011 10:37 pm

Re: [TUTORIAL] Connecting to MySQL database

Postby marand » Sun Oct 16, 2011 10:23 pm

hi there! i m new here! the tutorial is very useful and understanding but......THE SAME PROBLEM!! only the key string url is displayed on the screen.... like many others i ve tried many tricks but i m still not able to see this app running!! :?

could anyone tell me what parameters am i supposed to have at this line of the php file :

i should run it like that? i also tried mysql_connect("","root",""); as i know that the root user doesn't have any password.. how can i find out the correct parameters for my pc and wamp server (sorry ,im a beginner)

and as for the java file, my key string is declared like that :
public static final String KEY_121 = ""; (this is the path my php file is located: C:\wamp\www)

it's seems quite disappointing to me that this issue haven't solved yet despite being discussed here for so long.... :cry:
pls.... helpppp! thnx in advance!!!!!!
Once Poster
Once Poster
Posts: 1
Joined: Sun Oct 16, 2011 10:01 pm

Re: [TUTORIAL] Connecting to MySQL database

Postby firzan » Thu Oct 20, 2011 12:31 pm

i want to develope a chat application like "WhatsApp"....
plZ help me...!!!
Once Poster
Once Poster
Posts: 1
Joined: Thu Oct 20, 2011 12:28 pm

Re: [TUTORIAL] Connecting to MySQL database

Postby Hellboundz » Thu Nov 03, 2011 2:17 am


I am having issues with my php script. When I execute it through my web browser. It shows the following:

Connected to MySQL
Connected to Database

I was wondering why the null is appearing. The code below displays my php code

Code: Select all


        echo  "Connected to MySQL<br>";


        echo "Connected to Database<br>";

   $q=mysql_query("SELECT * FROM tube WHERE code = B");




Any suggestions would be very helpful.
P.S My primary key is set to auto-incremental not sure if that is relevant.

Posts: 2
Joined: Thu Nov 03, 2011 2:11 am

Re: [TUTORIAL] Connecting to MySQL database

Postby Hellboundz » Tue Nov 15, 2011 1:49 am

shakey wrote:Hey Anybody

Please help me. I like many others have not gotten this to work, I will post my code tell me what I am doing wrong. I should mention that I am not using a local server. I have my php script on a webserver hosted by Host Gator. My DB is there also, although don't know where in the file directory it is housed. my code is below.

The Android Java File
Code: Select all
package com.connector;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class whitehead extends Activity {
/** Called when the activity is first created. */
   TextView txt;
public void onCreate(Bundle savedInstanceState) {
    // Create a crude view - this should really be set via the layout resources
    // but since its an example saves declaring them in the XML.
    LinearLayout rootLayout = new LinearLayout(getApplicationContext());
    txt = new TextView(getApplicationContext());

    // Set the text and call the connect function.
  //call the method to run the data retreival

public static final String KEY_121 = "http://www.xtremeware.net/StreetCred/test.php"; //i use my real ip here

private String getServerData(String returnString) {
   InputStream is = null;
   String result = "";
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year","1970"));

    //http post
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());

    //convert response to string
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    //parse json data
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", name: "+json_data.getString("name")+
                            ", sex: "+json_data.getInt("sex")+
                            ", birthyear: "+json_data.getInt("birthyear")
                    //Get an output to the screen
                    returnString += "\n\t" + jArray.getJSONObject(i);
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    return returnString;

The Php Script
Code: Select all
   mysql_connect("localhost:3306", "I put my user name here created by cpanel", "password given by cpanel")or die(mysql_error());
   echo  "Connected to MySQL<br>";
   echo "Connected to Database<br>";
   $q = mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
      $output[] = $e;

Should I be using localhost:3306 or the full path which is http://www.xtremeware.net/StreetCred/test.php.

Here is my manifest file
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".whitehead"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

<uses-permission android:name="android.permission.INTERNET" />

this is what I am getting in the LogCat
Code: Select all
05-27 22:01:11.698: ERROR/log_tag(15723): Error parsing data org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONArray

Please once I get this and figure it out I should be on my way, hopefully

@shakey the reason you are getting the Value Connected of type error is because if you look at the php coding it throws up "connected to mysql" and then "connected to database" which the code didn't take into to account to parse so if you remove those two lines from the php file you should be fine!!
Posts: 2
Joined: Thu Nov 03, 2011 2:11 am


Re: [TUTORIAL] Connecting to MySQL database

Postby june0627 » Wed Nov 23, 2011 9:05 am

i cant see the tutorial @@
Once Poster
Once Poster
Posts: 1
Joined: Wed Nov 23, 2011 8:58 am

Re: [TUTORIAL] Connecting to MySQL database

Postby ajay1 » Wed Dec 21, 2011 1:13 pm

Can anyone tell me where i have to place php file so that i can access MYSQL database through that file to my android application .

Once Poster
Once Poster
Posts: 1
Joined: Wed Dec 21, 2011 12:47 pm

Re: [TUTORIAL] Connecting to MySQL database

Postby hufnmouth » Mon Mar 19, 2012 5:16 am

This code is pretty buggy. If you are only seeing the URL to your PHP then the code is working to spec. The line "txt.setText(getServerData(KEY_121)); " returns the URL to the php code (KEY_121) and not the data to the database.
Once Poster
Once Poster
Posts: 1
Joined: Fri Jul 02, 2010 7:11 pm


Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 8 guests