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

save multidimensional array with foreach loop in database

我编写了使用 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());
}

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

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

$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 插入语句。但是上面的代码至少会给你留下一个包含多个记录的数组。

-或-

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

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

也可以使用以下格式通过一次查询完成:

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