gpt4 book ai didi

php - 解析 json 并通过 PHP 为 NULL 行更新 NULL

转载 作者:行者123 更新时间:2023-11-30 22:04:00 26 4
gpt4 key购买 nike

我正在解析我的 json 并将其放入 3 个新列,id2awnser2type2,但有些行为 NULL, 我如何制定一个条件来更新该行为空?(使id2,type2,awnser2 =某些行为空),这是我的 json 的一行:

[{"id":"26","answer":[{"option":"3","text":"HIGH"}],"type":"3"},
{"id":"30","answer":[{"option":"3","text":"LOW"}],"type":"3"},
{"id":"31","answer":[{"option":"3","text":"LOW"}],"type":"3"}]

我的意思是,例如:我的第二行是 NULL 并且不是 Json,而且我的输出有错误,这是我的代码:

<?php
$con=mysqli_connect("localhost","root","","array");
mysqli_set_charset($con,"utf8");

// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
$jason_array = json_decode($json,true);
// id2
$id = array();
foreach ($jason_array as $data) {
$id[] = $data['id'];
}
$ids= implode(',',$id);
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);

// type2
$type = array();
foreach ($jason_array as $data) {
$type[] = $data['type'];
}
$types= implode(',',$type);
$sql2="update user_survey_start set type2='$types' where us_id=".$row[1];//run update sql
echo $sql2."<br>";
mysqli_query($con,$sql2);
// awnser2
$answers = array();
foreach ($jason_array as $data) {
foreach($data['answer'] as $ans){
$answers[] =$ans['text'] ;
}
}
$answers= implode(',',$answers);
$sql3="update user_survey_start set awnser2='$answers' where us_id=".$row[1];//run update sql
echo $sql3."<br>";
mysqli_query($con,$sql3);
}
}
mysqli_close($con);
?>

最佳答案

谢谢@gaurav,我的问题在聊天中解决了, if(!is_null($json)){

<?php
$con=mysqli_connect("localhost","root","","array");
mysqli_set_charset($con,"utf8");

// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){

$jason_array = json_decode($json,true);

// id2
$id = array();
foreach ($jason_array as $data) {
$id[] = $data['id'];
}
$ids= implode(',',$id);
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);

// type2
$type = array();
foreach ($jason_array as $data) {
$type[] = $data['type'];
}
$types= implode(',',$type);
$sql2="update user_survey_start set type2='$types' where us_id=".$row[1];//run update sql
echo $sql2."<br>";
mysqli_query($con,$sql2);
// awnser2
$answers = array();
foreach ($jason_array as $data) {
foreach($data['answer'] as $ans){
$answers[] =$ans['text'] ;
}
}
$answers= implode(',',$answers);
$sql3="update user_survey_start set awnser2='$answers' where us_id=".$row[1];//run update sql
echo $sql3."<br>";
mysqli_query($con,$sql3);
}
}
}
mysqli_close($con);
?>

关于php - 解析 json 并通过 PHP 为 NULL 行更新 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360263/

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