使用 json 文件中的数据更新 mysql 表

发布于 2024-10-31 04:20:04 字数 557 浏览 1 评论 0原文

我对 json 完全陌生,因此很难使用。

我有一个 id 列表存储在 mysql 表的 id 字段中。

例如 id 字段包含这些值

  1. 100001
  2. 100002
  3. 100003

json 文件包含这些 id 的属性,如下所示。

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}

我的目的是使用 id 作为 where 子句中的条件,使用 json 文件中的数据更新 mysql 表中的姓名、年龄和地址字段。

但我不知道从哪里开始。

知道他们在做什么的人可以指导我吗?

提前致谢。

I am complete new to json and thefore struggling to work with.

I have a list of id's stored in a id field of a mysql table.

For example id field contains these values

  1. 100001
  2. 100002
  3. 100003

The json file containes properties of these id's, like this.

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}

My intention is to update the name, age and address fields in the mysql table with the data in json file using the id as a conditon in the where clause.

But I have no idea where to even start.

Can someone who know's what they doing, please direct me.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2024-11-07 04:20:04

你很难理解什么? json_decode() http://php.net/manual/en/function.json- decode.php

$var = '{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}';

print_r(json_decode($var));

将为您提供一个数组,您可以使用 foreach 并创建单独的更新语句或构建一个大型更新语句并执行它。

what you struggling to understand? json_decode() http://php.net/manual/en/function.json-decode.php

$var = '{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}';

print_r(json_decode($var));

that will give you an array you can either foreach though and create separate update statements of build one large update statement and execute it.

甜是你 2024-11-07 04:20:04

你应该在某个地方遇到错误;你的大括号不匹配,并且在年龄之后缺少右引号:

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},

应该是

{"100001":{"person":{"name":"John","age":"32","address":"123 street"}}},

您缺少实际 ID 的右大括号。为了更容易阅读,请尝试如下格式:

{
 "100001":{
           "person":{
                     "name":"John",
                     "age":"32",
                     "address":"123 street"
                     }
           }
 },

You SHOULD be getting an error somewhere; your curly braces don't match and you're missing closing quotes after the age:

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},

should be

{"100001":{"person":{"name":"John","age":"32","address":"123 street"}}},

You're missing the closing brace for the actual ID. To make it easier to read, try formatting like this:

{
 "100001":{
           "person":{
                     "name":"John",
                     "age":"32",
                     "address":"123 street"
                     }
           }
 },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文