query("SET CHAR-6ren">
gpt4 book ai didi

php - 从 MySql Android 中的多个表中获取数据

转载 作者:行者123 更新时间:2023-11-29 02:46:22 25 4
gpt4 key购买 nike

我想在 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 代码一起工作

主要 Activity

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

}

最佳答案

这有点猜测,但我认为这就是你想要的:

//...
$table1 = Array();
$table2 = Array();
$sql = "SELECT * FROM table1 ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows >0) {
// get the result for first table and save it into an array ($table1)
while($row = $result->fetch_assoc()) {
$table1[] = $row;
}
} else {
echo "0 results";
}

// just do the same for any other table
$sql = "SELECT * FROM table2 ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$table2[] = $row;
}
} else {
echo "0 results";
}
// here you put together what you wanna return:
$return = Array('table1' => $table1,
'table2' => $table2);
// encode it
$json = json_encode($return,JSON_UNESCAPED_UNICODE);

echo $json;

但是:
最好将其封装到一个函数中:

//...
function getTable($conn, $tableName) {
$sql = "SELECT * FROM $tableName ORDER BY id DESC";
$result = $conn->query($sql);
$tableResult = Array();

if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$tableResult[] = $row;
}
}
return $tableResult;
}

$return = Array('table1' => getTable($conn, 'table1'),
'table2' => getTable($conn, 'table2')
);
$json = json_encode($return,JSON_UNESCAPED_UNICODE);

echo $json;

最后:有许多免费的数据库类可以为您做类似的事情。此外,您可能想看看 REST-API Kitss,它们也很容易找到 - 而且是免费的。你正在做的工作,已经完成了很多次——而且大多数都做得更好、更安全。但如果您仍在学习,最好再做一次。

关于php - 从 MySql Android 中的多个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com