[英]Can't access JSON object from MySQL fetch result, PHP
Context, I am doing this PHP project that has function update a product data, the situation is the product has an amount of data, after the update, I pack all the data as a JSON object and store into MySQL DB. 上下文,我正在执行这个具有更新产品数据功能的PHP项目,情况是产品具有大量数据,更新后,我将所有数据打包为JSON对象并存储到MySQL DB中。 the reason of packing everything into one JSON object and store as one MySQL row is: in this project, the different product has different attributes, so I think this would be a quick solution for a temporary storing data.
将所有内容打包到一个JSON对象中并存储为一个MySQL行的原因是:在该项目中,不同的产品具有不同的属性,因此我认为这是临时存储数据的快速解决方案。
Problem, when I fetch back certain JSON object from MySQL DB, I can not access JSON object, I used JSON decode, but it not works. 问题,当我从MySQL DB取回某些JSON对象时,我无法访问JSON对象,我使用JSON解码,但是它不起作用。
here is how I created JSON object 这是我创建JSON对象的方法
$product_INFO = (object)array(
"ean_number" => "$Static_eanNumber",
"product_name" => "$productName",
"product_brand" => "$Brand",
"product_weight_gr" => "$weight_GR",
"product_vatrate" => "$vat__Rate",
"product_supplier" => "$SUPPLIER",
"product_googleShoppingCategory" => "$GOOGLE_ShoppingCategory",
"LIGHTSPEED_INFO" => $LIGHTSPEED_product_info_array
);
and encode 并编码
$Data_in_JSON = json_encode($product_INFO);
query 询问
$write_JSON_object = "INSERT INTO editing ( eannumber, json) VALUES ('$Static_eanNumber','$Data_in_JSON')";
in mysql database table, the column for JSON is Name: json Type: LongText Collation: utf8mb4_bin 在mysql数据库表中,JSON列为名称:json类型:LongText归类:utf8mb4_bin
when I fetch from the database, I use this query 当我从数据库中获取时,我使用此查询
$find = "SELECT json FROM editing WHERE eannumber = 10000007 ";
and 和
if (($found->num_rows) == 1) {
$mysqlJSON = mysqli_fetch_array($found)[0];
echo "<pre>";
print_r($mysqlJSON);
echo "</pre>";
}
This is the result I see on an HTML page 这是我在HTML页面上看到的结果
{"ean_number":"10000007",
"product_name":"somevalue",
"product_brand":"Leba",
"product_weight_gr":"65000",
"product_vatrate":"0.21",
"product_supplier":"LebaBenelux",
"product_googleShoppingCategory":"",
"LIGHTSPEED_INFO": [{"product_shop_id":68295980,"product_price":"2854.9533"..........
I tried to use JSON decode the MySQL fetch result but it not works, So I cannot access this result by their keys such as "ean_number", 我尝试使用JSON解码MySQL提取结果,但无法正常工作,因此无法通过其键(例如“ ean_number”)访问此结果,
print_r(json_decode($mysqlJSON)->ean_number);
I cannot tell where did I made mistake, I create a JSON object in the wrong way? 我无法确定我在哪里犯了错误,我以错误的方式创建了JSON对象? something wrong with MySQL or how I fetch data.
MySQL或我如何获取数据有问题。
Please help thanks!!! 请帮忙谢谢!!!
Your code is working for me but you can also change json_decode($mysqlJSON)->ean_number
to json_decode($mysqlJSON)
and then access the property you want: 您的代码对我
json_decode($mysqlJSON)->ean_number
但是您也可以将json_decode($mysqlJSON)->ean_number
为json_decode($mysqlJSON)
,然后访问所需的属性:
$json = '{"ean_number":"10000007",
"product_name":"somSomething is wrongue",
"product_brand":"Leba",
"product_weight_gr":"65000",
"product_vatrate":"0.21",
"product_supplier":"LebaBenelux",
"product_googleShoppingCategory":""}';
$decoded = json_decode($json);
echo $decoded->ean_number;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.