将DOMXpath查询插入MySQL数据库

时间:2015-11-02 18:40:32

标签: php

我使用这个DOMXpath查询:

$html = file_get_contents("http://localhost:8888/stockPrices.php");

libxml_use_internal_errors(true);

$doc = new \DOMDocument();

if($doc->loadHTML($html))
{
    $result = new \DOMDocument();
    $result->formatOutput = true;
    $table = $result->appendChild($result->createElement("table"));
    $thead = $table->appendChild($result->createElement("thead"));
    $tbody = $table->appendChild($result->createElement("tbody"));

    $table->setAttribute('class', 'table table-hover');

    $xpath = new \DOMXPath($doc);

    $newRow = $thead->appendChild($result->createElement("tr"));

    foreach($xpath->query("//table[@id='kurstabell']/thead/tr/th[position()>1]") as $header)
    {
        $newRow->appendChild($result->createElement("th", trim($header->nodeValue)));
    }

    foreach($xpath->query("//table[@id='kurstabell']/tbody/tr") as $row)
    {
        $newRow = $tbody->appendChild($result->createElement("tr"));

        foreach($xpath->query("./td[position()>1]", $row) as $cell)
        {
            $newRow->appendChild($result->createElement("td", trim(htmlentities($cell->nodeValue))));
        }
    }

    echo $result->saveXML($result->documentElement);
}

此查询生成此300行+ HTML表:

+-----------+-----------+--------+--------+--------+--------+--------+--------+------------+----------+--------+
|   Stock   | PrevClose |  +/-   |   %    |  Bid   |  Ask   |  High  |  Low   |   Volume   | Currency |  Time  |
+-----------+-----------+--------+--------+--------+--------+--------+--------+------------+----------+--------+
| Appel Inc | 119.50    | 1,03   | -0,85% | 119.42 | 119.60 | 120    | 118    | 49,365,254 | USD      | 13:00  |
| Fake Inc  | 55        | 3      | 0,05   | 54     | 55,5   | 56     | 52     | 1,231,32   | USD      | 13:00  |
| etc...    | etc...    | etc... | etc... | etc... | etc... | etc... | etc... | etc...     | etc...   | etc... |
+-----------+-----------+--------+--------+--------+--------+--------+--------+------------+----------+--------+

我试图将这些列插入到带

的数组中
foreach($xpath->query as $key => $value) {
 $data = [
       'stock' => trim($value->getElementsByTagName('tr')->item(0)->nodeValue),
       'prevclose' => trim($value->getElementsByTagName('tr')->item(1)->nodeValue),
       'high' => trim($value->getElementsByTagName('tr')->item(6)->nodeValue),
       'low' => trim($value->getElementsByTagName('tr')->item(7)->nodeValue),
       'volume' => trim($value->getElementsByTagName('tr')->item(8)->nodeValue),
       'timestamp' => NOW()
        ];
}
echo "<pre>";
print_r($data);
echo "</pre>";

使用

将结果循环到数据库中
$sql = "INSERT INTO stock_list (`stock`, `prevclose`, `high`, `low`, `volume`, `timestamp`) VALUES
        (:stock, :prevclose, :high, :low, :volume, :timestamp)";
$query->database->prepare($sql);

foreach($data as $stock){
$query->execute(array(':stock' => $stock->stock,
                      ':prevclose' => $stock->prevclose,
                      ':high' => $stock->high,
                      ':low' => $stock->low,
                      ':volume' => $stock->volume,
                      ':timestamp' => NOW()
                      ));
}

var_dumpprint_r $ data都返回null

我的问题:

如何插入股票 PrevClose 音量进入mysql表?

0 个答案:

没有答案
相关问题