[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?
thanujalakshmi
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Sep 29, 2011 8:32 am

Top

Re: [TUTORIAL] Connecting to MySQL database

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

Hello.

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.
Luka
luxi99
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 :
mysql_connect("127.0.0.1","root","xxpasswordxx");

i should run it like that? i also tried mysql_connect("127.0.0.1","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 = "http://10.0.2.2/getAllPeopleBornAfter.php"; (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!!!!!!
marand
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...!!!
firzan
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

Hi,

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
null

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

Code: Select all
<?php
   define('dbname',"dbname");
   define('dbusername',"dbusername");
   define('dbpass',"pass");
   define('dbhost','10.10.10.1');

   mysql_connect(dbhost,dbusername,dbpass);

        echo  "Connected to MySQL<br>";

        mysql_select_db(dbname);

        echo "Connected to Database<br>";

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

   while($row=mysql_fetch_assoc($sql))
           $output[]=$row;

   print(json_encode($output));

   mysql_close();
?>


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

Thanks
Hellboundz
Freshman
Freshman
 
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;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // 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());
    rootLayout.addView(txt);
    setContentView(rootLayout);

    // Set the text and call the connect function.
    txt.setText("Connecting...");
  //call the method to run the data retreival
    txt.setText(getServerData(KEY_121));



}
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
    try{
            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
    try{
            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");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    //parse json data
    try{
            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
<?php
   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>";
   mysql_select_db("shakey_PeopleData");
   echo "Connected to Database<br>";
   
   $q = mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
   while($e=mysql_fetch_assoc($q))
      $output[] = $e;
   print(json_encode($output));
   mysql_close();
?>


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"
      package="com.connector"
      android:versionCode="1"
      android:versionName="1.0">
   <uses-sdk android:minSdkVersion="8" />
   

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

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


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!!
Hellboundz
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 03, 2011 2:11 am

Top

Re: [TUTORIAL] Connecting to MySQL database

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

i cant see the tutorial @@
june0627
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

Hi,
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 .

Regards,
ajay.
ajay1
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.
hufnmouth
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 02, 2010 7:11 pm

Top
Previous

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests