在php中循环多维数组并执行mysql插入(股票数据)
我有一个多维数组,我希望循环抛出并为数组中的值插入mysql数据库 我需要插入到sql查询中的数组值包括:在php中循环多维数组并执行mysql插入(股票数据),php,mysql,arrays,Php,Mysql,Arrays,我有一个多维数组,我希望循环抛出并为数组中的值插入mysql数据库 我需要插入到sql查询中的数组值包括: candles[0][complete], candles[0][volume], candles[0][mid][h], candles[0][mid][l], candles[0][mid][c] candles[1][complete], candles[1][volume], candles[1][mid][h], candles[1][mid][l], candles[1][
candles[0][complete], candles[0][volume], candles[0][mid][h], candles[0][mid][l], candles[0][mid][c]
candles[1][complete], candles[1][volume], candles[1][mid][h], candles[1][mid][l], candles[1][mid][c]
candles[2][complete], candles[2][volume], candles[2][mid][h], candles[2][mid][l], candles[2][mid][c]
阵列:
$get_instruments_candles = Array
(
[instrument] => EUR_USD
[granularity] => H4
[candles] => Array
(
[0] => Array
(
[complete] => 1
[volume] => 4491
[time] => 2009-01-01T22:00:00.000000000Z
[bid] => Array
(
[o] => 1.40390
[h] => 1.40515
[l] => 1.39445
[c] => 1.39540
)
[mid] => Array
(
[o] => 1.40440
[h] => 1.40565
[l] => 1.39470
[c] => 1.39565
)
[ask] => Array
(
[o] => 1.40490
[h] => 1.40615
[l] => 1.39495
[c] => 1.39590
)
)
[1] => Array
(
[complete] => 1
[volume] => 6866
[time] => 2009-01-02T02:00:00.000000000Z
[bid] => Array
(
[o] => 1.39545
[h] => 1.39690
[l] => 1.38447
[c] => 1.38641
)
[mid] => Array
(
[o] => 1.39570
[h] => 1.39715
[l] => 1.38472
[c] => 1.38666
)
[ask] => Array
(
[o] => 1.39595
[h] => 1.39740
[l] => 1.38497
[c] => 1.38691
)
)
[2] => Array
(
[complete] => 1
[volume] => 12529
[time] => 2009-01-02T06:00:00.000000000Z
[bid] => Array
(
[o] => 1.38635
[h] => 1.39679
[l] => 1.38391
[c] => 1.39471
)
[mid] => Array
(
[o] => 1.38660
[h] => 1.39688
[l] => 1.38400
[c] => 1.39480
)
[ask] => Array
(
[o] => 1.38685
[h] => 1.39697
[l] => 1.38409
[c] => 1.39489
)
)
)
)
这个数组最多可以由5000个candle[]值组成,因此我需要找到在php中执行foreach或while循环的最佳方法来运行数组值并执行MySQL插入
// connect to mysql database
// Do a loop to create and insert the values into database
// Create sql Query to insert. [0] would be changed to arrays value 0,1,2,3,etc
$sql = "INSERT INTO table (complete, volume, high, low, close)
VALUES ('{candles[0][complete]}','{candles[0][volume]}','{candles[0][mid][h]}','{candles[0][mid][l]}','{candles[0][mid][c]}')";
您可以使用
foreach
循环,然后引用循环的当前元素。最好使用事先准备好的声明。以下是PDO版本:
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (:complete, :volume, :high, :low, :close)";
$stmt->bindParam(':complete', $complete);
$stmt->bindParam(':volumn', $volume);
$stmt->bindParam(':high', $high);
$stmt->bindParam(':low', $low);
$stmt->bindParam(':close', $close);
foreach ($get_instrument_candles['candles'] as $candle) {
$complete = $candle['complete'];
$volume = $candle['volume'];
$high = $candle['mid']['h'];
$low = $candle['mid']['l'];
$close = $candle['mid']['c'];
$stmt->execute();
}
mysqli版本如下所示:
$complete = $volume = $high = $low = $close = null;
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (?, ?, ?, ?, ?)";
$stmt->bind_param("iiiii", $complete, $volume, $high, $low, $close);
$sql = "INSERT INTO table (complete, volume, high, low, close) VALUES ";
foreach ($get_instrument_candles['candles'] as $candle) {
$sql .= "({$candle['complete']}, {$candle['volume']}, {$candle['mid']['h']}, {$candle['mid']['l']}, {$candle['mid']['c']}),";
}
// strip the trailing comma
$sql = substr($sql, 0, -1);
foreach
循环与PDO相同。您可以使用foreach
循环,然后引用循环的当前元素。最好使用事先准备好的声明。以下是PDO版本:
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (:complete, :volume, :high, :low, :close)";
$stmt->bindParam(':complete', $complete);
$stmt->bindParam(':volumn', $volume);
$stmt->bindParam(':high', $high);
$stmt->bindParam(':low', $low);
$stmt->bindParam(':close', $close);
foreach ($get_instrument_candles['candles'] as $candle) {
$complete = $candle['complete'];
$volume = $candle['volume'];
$high = $candle['mid']['h'];
$low = $candle['mid']['l'];
$close = $candle['mid']['c'];
$stmt->execute();
}
mysqli版本如下所示:
$complete = $volume = $high = $low = $close = null;
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (?, ?, ?, ?, ?)";
$stmt->bind_param("iiiii", $complete, $volume, $high, $low, $close);
$sql = "INSERT INTO table (complete, volume, high, low, close) VALUES ";
foreach ($get_instrument_candles['candles'] as $candle) {
$sql .= "({$candle['complete']}, {$candle['volume']}, {$candle['mid']['h']}, {$candle['mid']['l']}, {$candle['mid']['c']}),";
}
// strip the trailing comma
$sql = substr($sql, 0, -1);
foreach
循环与PDO相同。另一种方法(假设您信任输入数据)是生成单个大的INSERT
查询。最长的查询大小(设置为,MySQL<8.0.2的默认值为4MB)应该足以插入5000个蜡烛的数据。所以代码看起来像:
$complete = $volume = $high = $low = $close = null;
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (?, ?, ?, ?, ?)";
$stmt->bind_param("iiiii", $complete, $volume, $high, $low, $close);
$sql = "INSERT INTO table (complete, volume, high, low, close) VALUES ";
foreach ($get_instrument_candles['candles'] as $candle) {
$sql .= "({$candle['complete']}, {$candle['volume']}, {$candle['mid']['h']}, {$candle['mid']['l']}, {$candle['mid']['c']}),";
}
// strip the trailing comma
$sql = substr($sql, 0, -1);
如果您不能确定输入数据是否安全,则@Barmar的预处理语句方法是可行的。另一种方法(假设您信任输入数据)是生成一个大的INSERT
查询。最长的查询大小(设置为,MySQL<8.0.2的默认值为4MB)应该足以插入5000个蜡烛的数据。所以代码看起来像:
$complete = $volume = $high = $low = $close = null;
$stmt = $link->prepare("INSERT INTO table (complete, volume, high, low, close)
VALUES (?, ?, ?, ?, ?)";
$stmt->bind_param("iiiii", $complete, $volume, $high, $low, $close);
$sql = "INSERT INTO table (complete, volume, high, low, close) VALUES ";
foreach ($get_instrument_candles['candles'] as $candle) {
$sql .= "({$candle['complete']}, {$candle['volume']}, {$candle['mid']['h']}, {$candle['mid']['l']}, {$candle['mid']['c']}),";
}
// strip the trailing comma
$sql = substr($sql, 0, -1);
如果您不能确定输入数据是否安全,@Barmar的预处理语句方法是正确的。如果您将占位符名称与关联数组的结构对齐,您可以直接执行(
execute()
。@tadman它看起来像来自公共API的响应,因此,他可能对结构没有太多控制权。没错,不过如果这可以依赖,那就太好了。感谢您的快速回复,这确实解决了访问数组的问题。如果您将占位符名称与关联数组的结构对齐,您可以只执行execute()
直接反对。@tadman它看起来像是来自公共API的响应,因此他可能对结构没有太多控制权。没错,不过如果可以依赖它,那就太好了。感谢您的快速回复,这确实解决了访问ArrayId的问题。这些答案之一解决了您的问题?如果是,请将答案标记为已接受(向上/向下投票箭头下方的绿色箭头)。否则,您可以提供更多信息来帮助解决问题。看看这些答案中有没有一个解决了你的问题?如果是,请标记接受的答案(上/下投票箭头下方的绿色箭头)。否则,您可以提供更多信息来帮助解决问题。看看api数组是安全的,所以像你提到的那样进行插入是一个很好的选择,如果数据已经存储,我必须做一些检查,然后,只需像您提到的那样进行附加如果您在数据上有一个唯一的键,那么您可以使用插入忽略
,而不用担心检查它是否已经存储。api数组是安全的,所以像您提到的那样进行插入是一个不错的选择,如果数据已经存储,我必须进行一些检查,然后,如您所述,只需添加数据,如果数据上有唯一的键,则只需使用INSERT IGNORE
,而无需检查数据是否已存储。