dongpao1083 2016-05-07 20:27
浏览 68
已采纳

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

I'm currently working on a project that involves an Android app and 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. Now, here is my .php working

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

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
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 *")

Would my problem be solved by just creating another class that implements Serializable and has as attributes "Nombre" and "Cuenta"?

Thank you!

  • 写回答

1条回答 默认 最新

  • duanpi5733 2016-05-10 13:47
    关注

    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:

    <?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".

    And here you have my Java class

    Hope this helps somebody else! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置