-1

This is the php script I have written to get the data from a MySQL database but it gives the error mentioned later and also the app where I am using this script gives a timeouterror .

PHP Script:

<?php 

 define('HOST','***');
 define('USER','***');
 define('PASS','***');
 define('DB','champion_trial_database');


 $con = mysqli_connect('HOST', 'USER', 'PASS', 'DB');

 $sql ="SELECT * FROM `trial`";

 $r = mysqli_query($con,$sql);

 $res = mysqli_fetch_array($r);

 echo ("name:"+$res);

 mysqli_close($con);

 ?>

Main_activity.java file where a GET request is generated using the volley library is as follows:

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editTextId;
    private Button buttonGet;
    private TextView textViewResult;

    private ProgressDialog loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextId = (EditText) findViewById(R.id.editTextId);
        buttonGet = (Button) findViewById(R.id.buttonGet);
        textViewResult = (TextView) findViewById(R.id.textViewResult);

        buttonGet.setOnClickListener(this);
    }

    private void getData() {
        String id = editTextId.getText().toString().trim();
        if (id.equals("")) {
            Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
            return;
        }
        loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
        String url = "http://www.champion6346.5gbfree.com/getData.php";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET,url,

                new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                textViewResult.setText("Name: "+response);
               // showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
                        Log.d("TAG:","Error mode entered  ");
                        error.printStackTrace();
                    }
                });
        requestQueue.add(stringRequest);
    }
    @Override
    public void onClick(View v) {
        getData();
    }
}

This the error log of the server when I try to run the php script mentioned above:

[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/champion6346/public_html/getData.php on line 9
[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/champion6346/public_html/getData.php on line 9
[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysql_query(): Access denied for user ''@'localhost' (using password: NO) in /home/champion6346/public_html/getData.php on line 14
[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysql_query(): A link to the server could not be established in /home/champion6346/public_html/getData.php on line 14
[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/champion6346/public_html/getData.php on line 16
[13-Apr-2017 13:43:50 America/Denver] PHP Warning:  mysql_close() expects parameter 1 to be resource, boolean given in /home/champion6346/public_html/getData.php on line 20

2 Answers2

0

In this situation I'd also get sure the db hostname indeed resolves to some ip address (e.g. ping the hostname or use dig/nslookup tools). If the hostname does not resolve this is the exact problem. Otherwise I'd trace code or use a network sniffer (wireshark, tdpdump, etc) to get sure if the PHP code understands your demand and indeed connects to the hostname you've specified. Can't comment due to low rating, thus answering.

kshpolvind
  • 91
  • 4
0

Here are some fixes applied to your php code

<?php 

 define('HOST','****');
 define('USER','****');
 define('PASS','****');
 define('DB','champion_trial_database');


 $con = mysqli_connect(HOST, USER, PASS, DB);

 $sql ="SELECT * FROM trial";

 $r = mysqli_query($con,$sql);

 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 echo "name:".$row['columnname'];
}

 mysqli_close($con);

 ?>

columnname is the name of the column, you look for values. You should adjust it.

UPDATE

You also have a connection problem, to solve it this answer might help https://stackoverflow.com/a/35608572

After you solved connection problem, code above should do what you need.

Community
  • 1
  • 1
Оzgur
  • 432
  • 2
  • 10
  • The problem is, he isn't even getting to displaying results. The errors show he can't connect – Derek Apr 13 '17 at 20:57
  • @Really-Brad-Larson thanks, updated my answer – Оzgur Apr 13 '17 at 21:14
  • Actually the issue is I have already written a code which simply connects with the server and displays a simple php script which goes like: ` ` .The code works sometimes and sometimes it does not work . So ,I am not sure whether there is an issue with my server or something else . @Ozgur @user4925913 – Champion6346 Apr 14 '17 at 09:20
  • @Champion6346 did you try my answer? Because you mysqli_connect syntax is wrong on your question. Constants should not be covered with single quotes. Otherwise It is normal to get getaddr failed error. Besides you had many more syntax errors, they are fixed on my answer. Can you please try and write here if tou get any errors? – Оzgur Apr 14 '17 at 10:37
  • @Ozgur I tried your code but it is giving the same error . – Champion6346 Apr 14 '17 at 15:38
  • @Champion6346 well, then I believe you have a problem with your server. Good luck with it – Оzgur Apr 14 '17 at 15:49
  • @Ozgur Could you tell me what exactly should i write in the HOST part of mysqli_connect as that might have been creating the problem ? I tested it by mentioning the domain name of my server . Ex: abcd.5gbfree.com:2082 – Champion6346 Apr 14 '17 at 16:11
  • @Champion6346 this is exactly why you get error, it is definitely not the domain name of your server. Try 'localhost' instead. If localhost does not work, you should ask to server admin – Оzgur Apr 14 '17 at 16:16
  • @Ozgur -Ok so the code finally worked but the response I get has nothing(its empty) ,although the database has elements in the column specified in the name ,what could be the possible reasson for this ? – Champion6346 Apr 14 '17 at 18:10
  • @Champion6346 just remove quotes wrapping word trial, FROM trial. Also replace columnname, with the column name from your table – Оzgur Apr 15 '17 at 21:49