gpt4 book ai didi

php - 如何将json数据存储到mysql

转载 作者:行者123 更新时间:2023-11-29 11:49:29 25 4
gpt4 key购买 nike

这段代码非常适合解析json对象,但我需要将所有json数据存储到mysql。如何检索要存储在 mysql 中的值?

function getDataAsArray($filename){
$dataArray=array();
$contents = file_get_contents($filename);
$first_split=explode("}}{",$contents);
$json_array=explode("\n",$first_split[0]);
foreach($json_array as $value){
$data = json_decode($value, true);
$dataArray[]=$data;
}
return $dataArray;
}
$logDataArray=getDataAsArray("akshara.txt");
echo "<pre>";print_r($logDataArray); echo "</pre>"

最佳答案

MySQL supports a JSON datatype 2015 年,就像 Postgres 几年来的情况一样。您只需将 JSON 插入数据库即可。

$json = file_get_contents($file); // {"my_value": "my data"}
DB::query("INSERT INTO my_table (json) VALUES ('{$json}')");

然后您可以使用 the various JSON functions查询您插入的数据。

SELECT JSN_EXTRACT(json, '$.my_value') AS my_value FROM my_table // my_value = my data

关于php - 如何将json数据存储到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com