如何从PHP中获取MySQL数据库中的数据?

时间:2017-04-13 20:34:18

标签: php android

这是我编写的用于从MySQL数据库获取数据的PHP脚本,但它提供了后面提到的错误以及我使用此脚本的应用程序给出了时间错误。

PHP脚本:

<?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);

 ?>

使用排球库生成GET请求的Main_activity.java文件如下:

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();
    }
}

当我尝试运行上面提到的php脚本时,这是服务器的错误日志:

[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 个答案:

答案 0 :(得分:0)

在这种情况下,我还要确保db主机名确实解析为某个IP地址(例如ping主机名或使用dig / nslookup工具)。如果主机名没有解决,这是确切的问题。否则,我会跟踪代码或使用网络嗅探器(wireshark,tdpdump等)来确定PHP代码是否了解您的需求并确实连接到您指定的主机名。 由于评分较低而无法评论,因此回答。

答案 1 :(得分:0)

以下是一些应用于您的PHP代码的修补程序

<?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是列的名称,您查找值。你应该调整它。

<强>更新

您还遇到连接问题,要解决这个问题,这个答案可能会有所帮助https://stackoverflow.com/a/35608572

解决连接问题后,上面的代码应该可以满足您的需求。