Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:00:37+00:00 2026-05-30T18:00:37+00:00

I am trying to connect to MySQL DB using php script. But I don’t

  • 0

I am trying to connect to MySQL DB using php script. But I don’t get any output only exception code. I can’t figure it out where is the problem. I used a tutorial code.

private EditText outputStream;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String result = null;
    InputStream input = null;
    StringBuilder sbuilder = null;
    outputStream = (EditText)findViewById(R.id.output);
    ArrayList <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>();

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/Bandymas/index.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        input = entity.getContent();
    }
    catch(Exception e){
        Log.e("log_tag","Error in internet connection"+e.toString());
    }
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(input,"iso-8859-1"),8);
        sbuilder = new StringBuilder();

        String line = null;

        while((line = reader.readLine()) != null){
            sbuilder.append(line + "\n");
            System.out.println(line);
        }
        input.close();
        result = sbuilder.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());          
    }
    int fd_id;
    String fd_name;
    try{
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data = null;
        for(int i=0;i<jArray.length();i++){
            json_data = jArray.getJSONObject(i);
            fd_id = json_data.getInt("FOOD_ID");
            fd_name = json_data.getString("FOOD_NAME");
            outputStream.append(fd_id +" " + fd_name + "\n");
        }


        }
    catch(JSONException e1){
        Toast.makeText(getBaseContext(), "No food found", Toast.LENGTH_LONG).show();
    }
    catch(ParseException e1){
        e1.printStackTrace();
    }
}

PHP script:

<?php
mysql_connect("localhost","**********","******");
mysql_select_db("test");
$sql = mysql_query("select FOOD_NAME as 'Maistas' from FOOD where FOOD_NAME like 'A%'");
while($row = mysql_fetch_assoc($sql)) $output[]=$row;
print(json_encode($output));
mysql_close;

?>

Any ideas how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:00:39+00:00Added an answer on May 30, 2026 at 6:00 pm

    First, dont use Exception.toString(), use Exception.printStackTrace():

    catch (Exception e) {
        e.printStackTrace();
    }
    

    Second, in your PHP code, your not checking for any errors. If any errors occur, I suggest you issue a different HTTP status code (like 400), then, in your Android code:

    if (response.getStatusLine().getStatusCode() != 200) {
        Log.d("MyApp", "Server encountered an error.);
    }
    

    This way you will know if something happened on the server.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to connect to mysql all evening using PHP. I know
I've been trying to connect to a mysql db through a php script. All
I am trying to display from a MySQL Database using PHP and Ajax but
I am trying to connect to MySQL using PHP from an Android Device. I
Using PHP and MySQL in a script run on mysite1.com I'm trying to copy
I am trying to connect to a remote MySQL database using Visual C# 2008
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I'm trying to set up a login script for PHP using the tutorial on
I'm trying to connect to a mysql server at dreamhost from a php scrip
Help! I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.