Php json 插入到 mysql 数据并插入控制限制

Php json insert to mysql data and insert control limits

提问人:K.biciukas 提问时间:10/15/2018 最后编辑:K.biciukas 更新时间:10/15/2018 访问量:51

问:

嘿,这个脚本在 json 8 或更多时运行良好,但是当它低于 8 时,比如 6,我得到错误。我需要输出从 0-8 获取插入,而不仅仅是 8。youtube#video

$loop = mysqli_query($conn, "SELECT channelid FROM users ORDER BY id") or die (mysqli_error($conn));

while ($row = mysqli_fetch_array($loop)) {
    $channelid = $row['channelid'];
    $url = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelid .'&maxResults=10&key=KEY';
    $content = file_get_contents($url); 
    $json = json_decode($content, true);    

    if(!isset($json['items'])) continue; //skip if no items

    $videos = ['videoId'=>'','videoId1'=>'','videoId2'=>'','videoId3'=>'','videoId4'=>'','videoId5'=>'','videoId6'=>'','videoId7'=>''];
    $videossw = ['vidname'=>'','vidname1'=>'','vidname2'=>'','vidname3'=>'','vidname4'=>'','vidname5'=>'','vidname6'=>'','vidname7'=>''];
    $videoss = ['publishedAt'=>'','publishedAt1'=>''];

    $i = 0;
    //if(isset($videoList[$i]["id"]["videoId"])) {
    foreach($json['items'] as $items) {
        if ($items['id']['kind'] != 'youtube#playlist') {
            $keyy  = 0==$i ? 'publishedAt' : 'publishedAt'.$i;
            $videoss[$keyy] = $items['snippet']['publishedAt'];

            $keyyy  = 0==$i ? 'vidname' : 'vidname'.$i;
            $videossw[$keyyy] = $items['snippet']['title']; 

            $key  = 0==$i ? 'videoId' : 'videoId'.$i;
            $videos[$key] = $items['id']['videoId']; 
            ++$i;
        }
    }
    $qqq = 'INSERT INTO users(channelid, publishedAt, publishedAt1, videoId, videoId1, videoId2, videoId3, videoId4, videoId5, videoId6, videoId7, vidname, vidname1, vidname2, vidname3, vidname4, vidname5, vidname6, vidname7) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE channelid=?, publishedAt=?, publishedAt1=?, videoId=?, videoId1=?, videoId2=?, videoId3=?, videoId4=?, videoId5=?, videoId6=?, videoId7=?, vidname=?, vidname1=?, vidname2=?, vidname3=?, vidname4=?, vidname5=?, vidname6=?, vidname7=?';
    $stmt = $conn->prepare($qqq);
    $stmt->bind_param('ssssssssssssssssssssssssssssssssssssss', $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videos['videoId5'], $videos['videoId6'], $videos['videoId7'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4'], $videossw['vidname5'], $videossw['vidname6'], $videossw['vidname7'], $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videos['videoId5'], $videos['videoId6'], $videos['videoId7'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4'], $videossw['vidname5'], $videossw['vidname6'], $videossw['vidname7']);
    $stmt->execute();
}

var_dump

array(2) {
  ["kind"]=>
  string(13) "youtube#video"
  ["videoId"]=>
  string(11) "ElNBG63UQ0g"
}
array(2) {
  ["kind"]=>
  string(13) "youtube#video"
  ["videoId"]=>
  string(11) "e3PlqifTrS0"
}
<br />
<b>Notice</b>:  Undefined index: videoId in <b>/index.php</b> on line <b>63</b><br />
array(2) {
  ["kind"]=>
  string(15) "youtube#channel"
  ["channelId"]=>
  string(24) "UCynfZM0Edr9cA4pDymb2rEA"
}

在线$videos[$key] = $items['id']['videoId'];

我收到错误:

注意:未定义的索引:videoId

php mysql mysqli foreach 插入

评论

0赞 delboy1978uk 10/15/2018
var_dump($items['id'])看看你得到了什么
0赞 K.biciukas 10/15/2018
用var_bump更新了问题
1赞 daremachine 10/15/2018
这是因为您的数组不包含此键。你有不同的键在循环中。您应该检查数组键是否存在,然后执行某些操作。

答: 暂无答案