title: Inserting data to Mysql from Youtube api
body: <p>I am trying to figure out how to insert all rows of video info from calls to the Youtube api using curl. I am not sure what I am doing wrong but no matter what I try I am only able to insert a single row.
  Here is a simple example of one of the ways I have tried with a random playlist.</p>
  
  <pre><code>$con=mysqli_connect("localhost","...","...","...");
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  $url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id%2C+status&amp;playlistId=PLU12uITxBEPHfZZRTIk96NduwU_8hT-Yo&amp;maxResults=10&amp;key={API KEY}";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0'
  ));
  
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
  curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  
  $json_response = curl_exec($ch);
  curl_close ($ch);
  
  $result = json_decode($json_response, true);
  
  foreach ($result['items'] as $page_info) {
  
  $video_id = $page_info['snippet']['resourceId']['videoId'];
  $title = $page_info['snippet']['title'];
  
  mysqli_query($con, "INSERT INTO links (video_id, title) VALUES ('$video_id', '$title')");  
  
  }
  </code></pre>
  
  <p>I also tried to create an array of everything and tried to dump it to sql. I figured I would still get the same 1 row result but I tried anyway.</p>
  
  <pre><code>$values = array();
  foreach ($result['items'] as $page_info) {
  
  $video_id = $page_info['snippet']['resourceId']['videoId'];
  $title = $page_info['snippet']['title'];
  
  $values[] = "('$video_id', '$title')";
  }
  $query_values = implode(',', $values);
  mysqli_query($con, "INSERT INTO links (video_id, title) VALUES $query_values");
  </code></pre>
  
  <p>Can anyone help me figure out what I am doing wrong? Thanks.</p>
  
  <p><strong>UPDATE:</strong><br>
  Turns out I had a problem with my table, I think it may have been corrupted or something. I created a new table and it solved my problem.</p>
  
  <p>To anyone that may see this and is wanting/trying to learn how to use the Youtube api and insert data into your own database, the code above in the first box works fine as of right now. </p>
  
  <p>Just change:</p>
  
  <pre><code>$con=mysqli_connect("localhost","...","...","...");
  </code></pre>
  
  <p>To your own db settings. Ex:</p>
  
  <pre><code>$con=mysqli_connect("localhost","Username","Password","Database");
  </code></pre>
  
  <p>And add the values you want to insert such as $video_id, $title and update the query to use the new values.<br>
  Example:   </p>
  
  <pre><code>$video_id = $page_info['snippet']['resourceId']['videoId'];
  $title = $page_info['snippet']['title'];
  $description = $page_info['snippet']['description'];
  mysqli_query($con, "INSERT INTO links (video_id, title, description) VALUES ('$video_id', '$title', '$description')");  
  </code></pre>
  
  <p>You also need to add your API key to your urls using key=.<br>
  Example:  </p>
  
  <pre><code>$url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id%2C+status&amp;playlistId=PLU12uITxBEPHfZZRTIk96NduwU_8hT-Yo&amp;maxResults=10&amp;key=123456789123456789";
  </code></pre>
  
  <p>You can get your free api key by signing up here <a href="https://console.developers.google.com/" rel="nofollow noreferrer">https://console.developers.google.com/</a> and then click library and select Youtube Data API.</p>
  
  <p>To learn how to build urls so that you can query whetever data you want then this should be a good place to start. <a href="https://developers.google.com/youtube/v3/sample_requests" rel="nofollow noreferrer">https://developers.google.com/youtube/v3/sample_requests</a></p>