php json插入mysql数据并插入控制限制

mnowg1ta  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(178)

嘿,这个脚本在json8或更高版本中运行得非常好 youtube#video 但当它低于8,比如6时,我会出错。我需要0-8的输出get insert,而不仅仅是8。

$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();
}

变量转储

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题