我有一个JSON文件,其中包含要存储在sql中两个不同数据库中的数据。
json文件如下:
[
{
"title": "CONSERVE IT LTD",
"address": "12 Truman Ave (10) ",
"phones": [
{
"name": "telephone_1",
"number": "876-754-0220"
},
{
"name": "telephone_2",
"number": "876-754-0221"
}
],
"website": "www.conserveitja.com",
"page": 1
},
{
"title": "Consie Walters Cancer Hospital",
"address": "22 Deanery Rd (3) ",
"phones": [
{
"name": "telephone_1",
"number": "876-930-5016"
}
],
"page": 1
},
...
]
我设法将标题,地址,网站和页面存储在数据库的第一个表中,但现在我想将每个企业的电话存储在同一数据库中的另一个表中。 >
这是我用来存储第一个表的信息的代码,而我只存储了第二个表的小代码。
$connect = mysqli_connect("localhost", "root", "1234", "businesses"); //Connect PHP to MySQL Database
$query = '';
$query_phones='';
$table_data = '';
$filename = "businesses.json";
$businesses = file_get_contents($filename);
$business = json_decode($businesses, true);
foreach($business as $row)
{
$query .= "INSERT INTO business(title, address, website, page) VALUES ('".$row["title"]."', '".$row["address"]."', '".$row["website"]."', '".$row["page"]."'); ";
//data that i will show on page
$table_data .= '
<tr>
<td>'.$row["title"].'</td>
<td>'.$row["address"].'</td>
<td>'.$row["website"].'</td>
<td>'.$row["page"].'</td>
</tr>
';
}
用于在数据库的第二个表中插入电话数据的代码
foreach($business as $row)
{
$query_phones .="INSERT INTO business_phones(business_title, phone_number, phone_name) VALUES ('".$row["title"]."', '".$row["number"]."', '".$row["name"]."');";
}
第一个表(业务)的结构
1 idPrimary int(11)
2 title varchar(50)
3 address varchar(50)
4 website varchar(50)
5 page int(11)
第二张桌子(business_phones)的结构
1 idPrimary int(11)
2 business_title varchar(250)
3 phone_number varchar(255)
4 phone_name varchar(255)