duandgfh0506 2013-01-18 03:37
浏览 23
已采纳

如何获取MySQL数据并将其读入Android应用程序

I have a website in which I have a very simple html form that I can fill out. The page was made using php and mysql, so it is also password protected. On this form, I accept input for "Data" and "Activity". With this very small system:

  • I can log in with a password
  • Enter a "Date" and "Activity"
  • The "Date" and "Activity" are stored in a MySQL database

At this point, I think I will just have to build a JSON parser, which there already are many tutorials on this. Such as this and this. what's confusing to me is how to create this JSON page. None of the tutorials really show you how to create a JSON array of the data you are looking for.

This may be a very basic question, but I still very new to Java, PHP, MySQL and totally new to JSON. Please educate me on this topic.

Update:

This code worked for me in the end

<?php
 $link = mysql_connect("localhost", "my_db", "password");

   mysql_select_db("my_table") or die(mysql_error());


$sql=mysql_query("select * from activity");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
}
print(json_encode($output));
mysql_close();
?>
  • 写回答

1条回答 默认 最新

  • dongyue934001 2013-01-18 03:52
    关注

    Try the built-in PHP function json_encode

    You can read about it here: http://php.net/manual/en/function.json-encode.php

    If you run a query from PHP like this: SELECT Activity, Date from YourTable where field = value

    Then you can use json_encode: echo json_encode($result)

    It will output something like this {"Activity":"Activity Text","Date":"ExampleDate"}

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

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程