【发布时间】:2017-01-13 00:18:34
【问题描述】:
我想在 android 应用程序中显示来自 mysql 的多个表我使用此 php 代码显示一个表
PHP
<?php
include 'config1.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->query("SET NAMES utf8");
$conn->query("SET CHARACTER SET utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM table1 ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows >0) {
// output data of each row
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem,JSON_UNESCAPED_UNICODE);
}
} else {
echo "0 results";
}
echo $json;
$conn->close();
?>
如何使用此代码显示多个表格
编辑: 我希望 php 代码与这个 Android 代码一起工作
主要活动
public void JSON_DATA_WEB_CALL(){
Intent intent = getIntent();
story_type = intent.getStringExtra("Story_Type");
String GET_JSON_DATA_HTTP_URL = "http://i-geeky.info/android/" + story_type + ".php";
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progress_layout.setVisibility(View.GONE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
ListItem_Rewayat GetDataAdapter2 = new ListItem_Rewayat();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setId(json.getString(id));
GetDataAdapter2.setName(json.getString(name));
GetDataAdapter2.seturl(json.getString(url));
GetDataAdapter2.setimg(json.getString(img));
GetDataAdapter2.setnum(json.getString(num));
GetDataAdapter2.setsize(json.getString(size));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapterRewayat = new Adapter_Rewayat(GetDataAdapter1, this);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewadapterRewayat);
}
列表项
public class ListItem_Rewayat {
public String id;
public String name;
public String url;
public String img;
public String num;
public String size;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getid() {
return id;
}
public void setId(String id1) {
this.id = id1;
}
public String geturl() {
return url;
}
public void seturl(String url1) {
this.url = url1;
}
public String getimg() {
return img;
}
public void setimg(String img1) {
this.img = img1;
}
public String getnum() {
return num;
}
public void setnum(String num1) {
this.num = num1;
}
public String getsize() {
return size;
}
public void setsize(String size1) {
this.size = size1;
}
}
【问题讨论】:
-
只需将其添加到查询中:
SELECT * FROM table1, table2 where table1.connector = table2.connector ORDER BY table1.id DESC这就是你的意思吗?? -
但您的代码只会发送表中的 last 项,因为您会在每一行中覆盖 $tem 和 $json。
-
您的 android 代码不适用于多个表。除非它们具有相同的字段,否则 php 需要不同。我们不知道您的桌子是什么样子的。
-
表有相同的字段