简体   繁体   English

使用JSON管理来自多个表的数据(PHP + MySQL)

[英]Using JSON to manage data from multiple tables (PHP + MySQL)

I'm currently working on a project that involves an Android app and MySQL. 我目前正在从事一个涉及Android应用程序和MySQL的项目。 I need to get data from a MySQL database (Which is located on a local Server, I'm using Apache), the thing is, I've have already done this by getting data from a SINGLE table and then with a class that implements "Serializable" I'm able to "use" the data. 我需要从MySQL数据库(位于本地服务器上,我正在使用Apache)中获取数据,事实是,我已经通过从SINGLE表中获取数据,然后使用实现“可序列化”我可以“使用”数据。 Now, here is my .php working 现在,这是我的.php文件

<?PHP
include_once("connection.php");

$query = "SELECT E.Nombre, COUNT(C.Id_Estudiante_FK) AS Cuenta
FROM Estudiante E INNER JOIN Comentario C ON E.Id_Estudiante=C.Id_Estudiante_FK
GROUP BY E.Id_Estudiante
ORDER BY COUNT(C.Id_Estudiante_FK) DESC"; 

$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
        $data[] = $row;
}
echo json_encode($data);
?>

It returns back the data I need already encoded, my question is how can I manage that information I'm getting, is it possible to just create another class that implements Serializable having as attributes the rows I'm returning (Which are "Nombre" and "Cuenta" )? 它返回我已经需要编码的数据,我的问题是我该如何管理我得到的信息,是否可以创建另一个实现Serializable的类,而该类具有我要返回的行作为属性(这是“ Nombre”“ Cuenta” )?

Here is the php I use for the query that only involves one table (Already works, and I'm able to "use" the data): 这是我用于查询的php,它仅涉及一个表(已经工作,并且我可以“使用”数据):

<?PHP
include_once("connection.php");

if(isset($_POST['txttipooferta'])){
$idtipooferta=$_POST['txttipooferta'];

$query = "SELECT * FROM oferta WHERE Id_Tipo_Oferta_FK=$idtipooferta AND Cupos>0 ORDER BY Id_Oferta DESC"; 

$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
        $data[] = $row;
}
echo json_encode($data);
}
?>

And here is the class that I use to manage the data that I get from the .php, note that the atributes of the class match the one the table "Oferta" has (as the .php's query is "SELECT *") 这是我用来管理从.php获取的数据的类,请注意,该类的属性与表“ Oferta”具有的属性匹配(因为.php的查询为“ SELECT *”)

Would my problem be solved by just creating another class that implements Serializable and has as attributes "Nombre" and "Cuenta" ? 我的问题是否可以通过创建另一个实现Serializable且具有属性“ Nombre”“ Cuenta”的类来解决?

Thank you! 谢谢!

As I stated in a comment before, this particular problem I had was a "plus" I would add to my project, I was focused on other more important parts of it therefore I did not have time to check on this. 正如我之前在评论中指出的那样,我遇到的这个特殊问题是我要添加到项目中的一个“加号”,我专注于该项目中其他更重要的部分,因此我没有时间进行检查。 The answer is yes, you can just create a class that implements Serializable and matches the attributes you are encoding, below you can see my .php and my Java Class that work: 答案是肯定的,您只需要创建一个实现Serializable并匹配您要编码的属性的类,就可以在下面看到我的.php和Java类起作用:

<?PHP
include_once("connection.php");

$query = "SELECT E.Nombre, COUNT(C.Id_Estudiante_FK) AS Cuenta
FROM Estudiante E INNER JOIN Comentario C ON E.Id_Estudiante=C.Id_Estudiante_FK
GROUP BY E.Id_Estudiante
ORDER BY COUNT(C.Id_Estudiante_FK) DESC"; 

$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
        $data[] = $row;
}
echo json_encode($data);
?>

The query returns two fields "Nombre" and "Cuenta". 该查询返回两个字段“ Nombre”和“ Cuenta”。

And here you have my Java class 这是我的Java课

Hope this helps somebody else! 希望这对其他人有帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM