title: Connecting mysql database with Android app
body: <p>I have a problem connecting database with Android app. I am trying to implement <a href="http://webdesignergeeks.com/mobile/android/android-login-authentication-with-remote-db/" rel="nofollow">this</a> tutorial. Everything seems to be fine but I neither get any success not an error.</p>
  
  <p>There is a button listener which on clicking does a post to a PHP file and gets the result. Here is the code for it:-</p>
  
  <pre><code>    ok.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
              postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
              postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
              //String valid = "1";
              String response = null;
              try {
                  response = CustomHttpClient.executeHttpPost("http://10.0.2.2/check.php", postParameters);
                  String res=response.toString();
                  Log.d("res:", res);
  
                 // res = res.trim();
                  res= res.replaceAll("\\s+","");                              
                  //error.setText(res);
  
                 if(res.equals("1"))
                      error.setText("Correct Username or Password");
                  else
                      error.setText("Sorry!! Incorrect Username or Password"); 
              } catch (Exception e) {
                  un.setText(e.toString());
              }
  
          }
      });
  </code></pre>
  
  <p>Here is the http post method:-</p>
  
  <pre><code>public static String executeHttpPost(String url, ArrayList&lt;NameValuePair&gt; postParameters) throws Exception {
      BufferedReader in = null;
      try {
          HttpClient client = getHttpClient();
          HttpPost request = new HttpPost(url);
          UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
          request.setEntity(formEntity);
          HttpResponse response = client.execute(request);
          in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  
          StringBuffer sb = new StringBuffer("");
          String line = "";
          String NL = System.getProperty("line.separator");
          while ((line = in.readLine()) != null) {
              sb.append(line + NL);
          }
          in.close();
  
          String result = sb.toString();
          Log.d("postMethodReturn", result);
          return result;
      } finally {
          if (in != null) {
              try {
                  in.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>The PHP code is as below:-</p>
  
  <pre><code>&lt;?php
  $un=$_POST['username'];
  $pw=$_POST['password'];
  //connect to the db
  $user = "xyz";
  $pswd = "xyz";
  $db = "mydb";
  $host = "localhost";
  $conn = mysql_connect($host, $user, $pswd);
  mysql_select_db($db);
  //run the query to search for the username and password the match  
  $query = "SELECT * FROM mytable WHERE user = '$un' AND pass = '$pw'";
  $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
  //this is where the actual verification happens  
  if(mysql_num_rows($result) --&gt; 0)  
  echo 1;  // for correct login response  
  else  
  echo 0; // for incorrect login response  
  ?&gt;
  </code></pre>
  
  <p>Is there any bug in the program? I tried logging the intermediate values of res (http response) in activity code and result in the execute post method, but nothing is being logged. Tried changing "localhost" to "127.0.0.1" and also into a publicly available webhost, with all the database environment, but no success. All these on emulator and with public host, tried with real device too. Server seems to be running when checked from browser. Database exists with the values. All services running (apache, mysql).</p>
  
  <p>The main problem is that there is no error! Any suggestions what is going wrong? 
  Couldn't find anyone with the same problem.</p>