Php 在MySql Android中从多个tahn one表获取数据
我想在android应用程序中显示来自mysql的多个表,我使用这段php代码显示了一个表 PHPPhp 在MySql Android中从多个tahn one表获取数据,php,android,mysql,Php,Android,Mysql,我想在android应用程序中显示来自mysql的多个表,我使用这段php代码显示了一个表 PHP 这有点猜测,但我认为这就是你想要的: //... $table1 = Array(); $table2 = Array(); $sql = "SELECT * FROM table1 ORDER BY id DESC"; $result = $conn->query($sql); if ($result->num_rows >0) { // get the result f
这有点猜测,但我认为这就是你想要的:
//...
$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;
最后:有许多免费的db类可以为您做类似的事情。此外,您可能还想看看REST-API套件,它们也很容易找到,而且是免费的。你正在做的工作,已经做了很多次了,而且大多数都做得更好、更安全。但是如果你还在学习,最好再做一次。只需将它添加到查询中:
SELECT*FROM table1,table2其中table1.connector=table2.connector ORDER BY table1.id DESC
这就是你需要的吗?但是你的代码只会发送表中的最后一项,因为您在每一行中都覆盖$tem和$json。您的android代码将无法处理多个表。除非它们有相同的字段,但是php需要不同。我们不知道您的表是什么样子。表有相同的字段不适合我我想要喜欢我的代码,因为它可以在android应用程序中与我的json代码一起工作,但如果需要更多表,请告诉我您的特殊需要!请编辑您的问题,您希望json的结构是什么。
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;