你好,我最近使用本教程创建了一个数据库:https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial
我需要将我的sql数据库转换成一个json数组(如下面所示),我正在使用手机间隙制作我的第一个移动应用程序,这个问题并不是关于电话间隔,但我怀疑提供一些上下文不会有什么影响。
我希望应用程序(只能使用html、css和js)向sql数据库发出请求--为我这样做的最佳方法是,一旦电话向json发出一个请求,就将整个数据库转换为json数组--新的json数据库将在当天的电话上缓存。
(即使这样的缓存是不可能的或不实用的,但在这个问题上并不重要)
我通过以下方法向数据库添加了三个“列表”:
INSERT INTO anime
(id
,category
,genre
,title
,description
,image_link
,date_added
) VALUES
(NULL
,"Mainstream"
,"Action"
,"the title"
,"the name"
,"200 word description"
,"imagename.png"
,'2012-04-14'
);我不确定这个方法是否以我想要的方式创建了数据库,因为我尝试过多次请求完整的信息,而且只有一种方法起作用,即我的PHP技能是垃圾。

我希望能产生一些类似这样的东西(任何更好的方式来格式化它,以及如何轻松地从它获取和使用信息),我一直在查看json_encode();并阅读过:How to build a JSON array from mysql database和其他堆栈。
categories: [
{
"category-name" : "Mainstream",
"category-description" : "Everyones watching them for a reason",
"category-link" : "#mainstream",
"Mainstream" : [
{
"genres" : [
{
"genre-name" : "Action",
"genre-description" : "Mainstream: Fight them off",
"list" : [
{
"img" : "www.ActionimglinkMainstream1.com",
"name" : "ActionMainstreamname1",
"description" : "this Action is Mainstream name 1"
},
{
"img" : "www.ActionimglinkMainstream2.com",
"name" : "ActionNewname2",
"description" : "this Action is Mainstream name 2"
},
{
"img" : "www.FantasyimglinkMainstream3.com",
"name" : "FantasyMainstreamname3",
"description" : "this Action is Mainstream name 3"
}
]
},
{
"genre-name" : "Fantasy",
"genre-description" : "Mainstream: I put a spell on you",
"list" : [
{
"img" : "www.FantasyimglinkMainstream1.com",
"name" : "FantasyMainstreamname1",
"description" : "this Fantasy is Mainstream name 1"
},
{
"img" : "www.ActionimglinkMainstream2.com",
"name" : "FantasyNewname2",
"description" : "this Fantasy is Mainstream name 2"
},
{
"img" : "www.FantasyimglinkMainstream3.com",
"name" : "FantasyMainstreamname3",
"description" : "this Fantasy is Mainstream name 3"
}
]
}
]
}
]
}]感谢您的时间,信息和洞察力!请理解,我对PHP、mysql和JavaScript都是新手,但不是google/stackoverflow,我已经搜索过并尝试过这样做,但它只是不起作用。
为了明确起见,我想知道如何将一个mySQL数据库隐藏到一个json数组中,我可以从一个html + js移动应用程序中请求这个数组。
要确认数据库是如何设置的:

发布于 2015-02-23 06:13:16
我建议把答案(对或错,不重要,投票将决定它)放入答案部分。因为它更容易读懂
不管怎样,试试这个。这是使用PDO (mysqli的替代方案)。
json_encode将其转换为JSON
$output = json_encode($result)echo,并且不要忘记将标题设置为JSON
标题(“内容-类型:应用程序/json”);回波(输出);注意,这个PHP返回JSON,而不是像@user1871802所建议的那样将它注入javascript
https://stackoverflow.com/questions/28627632
复制相似问题