使用数据库中的foreach循环保存多维数组

时间:2015-01-22 06:56:49

标签: php mysql arrays multidimensional-array

我警告脚本使用foreach循环从网站中提取价格和名称。

foreach ($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if ($row->item(0)->tagName != 'tblhead') { // avoid headers
        $data[] = array(
            $trip ['Name'] = trim($row->item(0)->nodeValue),
            $trip['LivePrice'] = trim($row->item(2)->nodeValue),
            $trip ['Changing'] = trim($row->item(4)->nodeValue),
            $trip ['Lowest'] = trim($row->item(6)->nodeValue),
            $trip['Topest'] = trim($row->item(8)->nodeValue),
            $trip['Time'] = trim($row->item(10)->nodeValue),
        );
    }
}

然后使用mysql(没有mysqli或pdo)将它们保存到数据库中。但只保存一条记录,如果提供5列。

每次刷新后页面保存上一条记录(即重复记录)

请参阅以下内容。

在数据库中:

id | name | liveprice | changing | lowest | topest | time 
1  |  lg  |  25500    |   05     |  22500 |  22500 |  2014
2  |  lg  |  25500    |   05     |  22500 |  22500 |  2014
3  |  lg  |  25500    |   05     |  22500 |  22500 |  2014

将数据存储到数据库的数据库代码和php

$article = array();
mysql_select_db("coin", $con);

"CREATE TABLE `Dadoo`(id INT NOT NULL AUTO INCREMENT,PRIMARY KEY(id),`title` VARCHAR(255),`liveprice` VARCHAR(255),`changing` VARCHAR(255),`lowest` VARCHAR(255),`topest` VARCHAR(255) ENGINE=MyISAM";

$debugquery = mysql_query("INSERT INTO `Dadoo`(title,liveprice,changing,lowest,topest,time) VALUES ('" . $trip['Name'] . "','" . $trip['LivePrice'] . "','" . $trip['Changing'] . "','" . $trip['Lowest'] . "','" . $trip['Topest'] . "','" . $trip['Time'] . "')");

if (!$debugquery) {
    die(mysql_error());
}

现在如何在数据库中保存多维数组?

2 个答案:

答案 0 :(得分:1)

你的数组需要在循环之外,否则每次循环时你都会覆盖每个索引:

$data = [];
$i = 0;
foreach($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if($row->item(0)->tagName != 'tblhead') { // avoid headers
        $data[i]['Name' ] = trim($row->item(0)->nodeValue);
        $data[i]['LivePrice'] = trim($row->item(2)->nodeValue);
        $data[i]['Changing'] = trim($row->item(4)->nodeValue);
        $data[i]['Lowest'] = trim($row->item(6)->nodeValue);
        $data[i]['Topest'] = trim($row->item(8)->nodeValue);
        $data[i]['Time'] = trim($row->item(10)->nodeValue);

    }
}

你也必须遍历你的sql insert语句。但上面的代码至少会给你一个包含多个记录的数组。

-OR -

最有效的方法可能只是生成insert语句并在循环中执行查询,而不是将值存储在数组中:

foreach($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if($row->item(0)->tagName != 'tblhead') { // avoid headers
        $sql = "INSERT INTO `Dadoo`(title,liveprice,changing,lowest,topest,time) VALUES ('" . trim($row->item(0)->nodeValue) . "','" . trim($row->item(2)->nodeValue) . "','" . trim($row->item(4)->nodeValue) . "','" . trim($row->item(6)->nodeValue) . "','" . trim($row->item(8)->nodeValue) . "','" . trim($row->item(10)->nodeValue) . "')");
        mysql_query($sql);
    }
}

答案 1 :(得分:1)

也可以使用以下格式进行一次查询:

INSERT INTO tablename (column1, column2, ...) 
VALUES (value01, value02, ...), (value11, value12, ...), ...

例如:

class InsertValues {

    private $values;
    private $row = array();

    public function endRow() {
        $this->values[] = '(' . implode(',', $this->row) . ')';
        $this->row = array();
    }

    public function addColumnValue($value, $quote = true) {
        $value = mysql_real_escape_string(trim($value)); //should use mysqli
        $this->row[] = $quote ? "'" . $value . "'" : $value;
    }

    public function render() {
        return implode(',', $this->values);
    }

}

mysql_select_db("coin", $con);

$inserValues = new InsertValues();

foreach ($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if ($row->item(0)->tagName != 'tblhead') { // avoid headers
        $inserValues->addColumnValue($row->item(0)->nodeValue);
        $inserValues->addColumnValue($row->item(2)->nodeValue);
        $inserValues->addColumnValue($row->item(4)->nodeValue);
        $inserValues->addColumnValue($row->item(6)->nodeValue);
        $inserValues->addColumnValue($row->item(8)->nodeValue);
        $inserValues->addColumnValue($row->item(10)->nodeValue);

        //above can be replaced with a for loop ;)

        $inserValues->endRow();
    }
}

$debugquery = mysql_query("INSERT INTO `Dadoo`(title,liveprice,changing,lowest,topest,time) "
        . "VALUES " . $inserValues->render());

if (!$debugquery) {
    die(mysql_error());
}