[英]Inserting JSON into MySQL Database
Firstly, here's an example of the JSON I have; 首先,这是我拥有的JSON的示例;
[["AAL.L","Anglo American plc"],["ABF.L","Associated British Foods plc"],
["ADM.L","Admiral Group plc"],["ADN.L","Aberdeen Asset Management PLC"]]
I'm trying to get create a loop to loop through the JSON and enter it into my MySQL database. 我正在尝试创建一个循环以循环遍历JSON并将其输入到我的MySQL数据库中。 The database has the following tables:
该数据库具有下表:
ID, Symbol, Company, Timestamp
ID is used as an auto_increment, and timestamp makes use of NOW()
in the MySQL statement. ID用作auto_increment,而时间戳在MySQL语句中使用
NOW()
。 The issue I'm having is that all the examples I've looked at decode JSON as a key/value pair and my JSON isn't. 我遇到的问题是,我看过的所有示例都将JSON解码为键/值对,而我的JSON不是。 The examples I've been looking at are the following:
我一直在看的示例如下:
mysql_query("INSERT INTO suspiciousactivity (ID,Notes)
VALUES ('".$arr[0]['a']."','".$arr[0]['b']."')")or die(mysql_error());
I'm not asking for the code to be written for me, but just an idea of what to research into. 我并不是要为我编写代码,而只是想研究什么。
Your JSON is just a list of lists, so you can just do something like: 您的JSON只是列表的列表,因此您可以执行以下操作:
<?php
$json ='[["AAL.L","Anglo American plc"],["ABF.L","Associated British Foods plc"],
["ADM.L","Admiral Group plc"],["ADN.L","Aberdeen Asset Management PLC"]]';
$items = json_decode($json); //Here items is an array of arrays
foreach($items as $item) {
echo $item[0]; //Symbol
echo $item[1]; //Company
//Here would be a good place to do your insert
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.