如何将其他网站的数据插入mysql?

时间:2016-03-14 11:30:47

标签: php mysql sql-insert

我想在周末学习一些新东西并决定自学PHPDOMMySQL。作为一个开始的项目,我想尝试将我从其他网站中提取的下表添加到我自己的数据库中。

Extract http://i67.tinypic.com/fuot5g.jpg http://i67.tinypic.com/fuot5g.jpg

但是,当我尝试将数据(不包括'No.''Ticker'等标题行)添加到我自己的数据库中时,我碰到了一堵墙。

以下是我的代码摘录:

<?php   

     $dom = new DOMDocument;
     $dom->loadHTML($document);

     $tbl = $dom->getElementByID('forex_performance');
     $trneeded = $tbl->getElementsByTagName('tr');

     foreach ($trneeded as $row) {
         foreach ($row->getElementsByTagName('td') as $cell) {
              $cellarray[] = $cell->nodeValue;
              $query = "INSERT INTO sampletable (no, ticker, price, perf5, perfhour, perfday, perfweek, perfmonth,perfquart, perfhalf, perfyear, perfytd, date, time) VALUES ('')
              mysql_query($query);
        } 
     }

?>

我能够从单元格数组中获取特定数据,但我不能在我的生活中找出如何将它们插入到我的数据库中。

我还是很陌生,希望我的问题有道理!如果有人能指点我正确的方向,我真的很感激!

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/nKD2hgTXHwAf_Y8YHghPkBlZJcs\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/GLcJdYlEeecN5amO819w0A1mSqU\"",
   "id": "PK_HFb8tkUs",
   "snippet": {
    "publishedAt": "2016-03-14T01:52:34.000Z",
    "channelId": "UCHqC-yWZ1kri4YzwRSt6RGQ",
    "title": "Full Speech: Donald Trump HUGE Rally in Boca Raton, FL (3-13-16)",
    "description": "Sunday, March 13, 2016: GOP Presidential candidate Donald Trump held a campaign rally in Boca Raton, FL at Sunset Cove Amphitheater and spoke to a massive crowd of tens of thousands of supporters.\n\nFull Speech: Donald Trump Rally in Boca Raton, FL (3-13-16)",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/PK_HFb8tkUs/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/PK_HFb8tkUs/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/PK_HFb8tkUs/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/PK_HFb8tkUs/sddefault.jpg",
      "width": 640,
      "height": 480
     }
    },
    "channelTitle": "Right Side Broadcasting",
    "tags": [
     "Donald Trump",
     "Live Stream",
     "Boca Raton",
     "Florida",
     "Rally",
     "Speech",
     "Politics",
     "Republican Party"
    ],
    "categoryId": "25",
    "liveBroadcastContent": "none",
    "localized": {
     "title": "Full Speech: Donald Trump HUGE Rally in Boca Raton, FL (3-13-16)",
     "description": "Sunday, March 13, 2016: GOP Presidential candidate Donald Trump held a campaign rally in Boca Raton, FL at Sunset Cove Amphitheater and spoke to a massive crowd of tens of thousands of supporters.\n\nFull Speech: Donald Trump Rally in Boca Raton, FL (3-13-16)"
    },
    "defaultAudioLanguage": "en"
   },
   "contentDetails": {
    "duration": "PT2H41M2S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true
   },
   "status": {
    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true
   },
   "statistics": {
    "viewCount": "371675",
    "likeCount": "7592",
    "dislikeCount": "586",
    "favoriteCount": "0",
    "commentCount": "1397"
   }
  }
 ]

答案 1 :(得分:0)

使用INSERT INTO SELECT:

INSERT INTO sampletable (no, ticker, price, perf5, perfhour, perfday, perfweek, perfmonth,perfquart, perfhalf, perfyear, perfytd, date, time) 
SELECT ... ;