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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c