gpt4 book ai didi

php - 多个数组插入数据库

转载 作者:行者123 更新时间:2023-11-29 13:07:22 25 4
gpt4 key购买 nike

我正在使用以下数组:

$name = array("Veldspar","Scordite","Pyroxeres","Plagioclase","Omber","Kernite","Jaspet","Hemorphite","Hedbergite","Gneiss","Dark Ochre","Crokite","Spodumain","Bistot","Arkanor","Mercoxit");
$typeids= array(1230,1228,1224,18,1227,20,1226,1231,21,1229,1232,1225,19,1223,22,11396);
$url="http://api.eve-central.com/api/marketstat?usesystem=30000142&typeid=".join('&typeid=',$typeids);
$pricexml=file_get_contents($url);
$xml=new SimpleXMLElement($pricexml);

foreach($typeids as $typeid)
{
$item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
$price= (float) $item[0]->buy->max;
$price=round($price,2);

$query1 = "INSERT INTO data (Price) VALUES ('$price');";

$q1 = mysqli_query($conn,$query1) or die ('Error posting data');
echo $typeids[$index].$name[$index].$price[$index]."\n";
}
foreach($typeids as $index => $value)
{
$query = "INSERT INTO data (typeID, Name) VALUES ('$typeids[$index]','$name[$index]');";

$q = mysqli_query($conn,$query) or die ('Error posting data');
echo $typeids[$index].$name[$index]."\n";

}

这是我玩的一款游戏,Eve online。我想要完成的是获取商品的价格/itemID/名称并将其注入(inject)我的数据库中。我可以在 2 个 foreach 循环之间获取所有 3 个项目。但是,当我将其放入数据库时​​,它会创建 2 组行。

+--------+-------------+-------+
| typeID | Name | Price |
+--------+-------------+-------+
| 0 | | 15 |
| 0 | | 27 |
| 0 | | 55 |
| 0 | | 58 |
| 0 | | 91 |
| 0 | | 227 |
| 0 | | 434 |
| 0 | | 740 |
| 0 | | 708 |
| 0 | | 914 |
| 0 | | 1505 |
| 0 | | 3202 |
| 0 | | 1600 |
| 0 | | 2900 |
| 0 | | 3180 |
| 0 | | 11800 |
| 1230 | Veldspar | 0 |
| 1228 | Scordite | 0 |
| 1224 | Pyroxeres | 0 |
| 18 | Plagioclase | 0 |
| 1227 | Omber | 0 |
| 20 | Kernite | 0 |
| 1226 | Jaspet | 0 |
| 1231 | Hemorphite | 0 |
| 21 | Hedbergite | 0 |
| 1229 | Gneiss | 0 |
| 1232 | Dark Ochre | 0 |
| 1225 | Crokite | 0 |
| 19 | Spodumain | 0 |
| 1223 | Bistot | 0 |
| 22 | Arkanor | 0 |
| 11396 | Mercoxit | 0

我尝试将所有 3 件事合并到 1 个 foreach 循环中,但它没有遵循 API 调用来获取价格,这就是为什么我产生了 2 个不同的 foreach 循环。

我当前想到的解决方案是合并行,是否可以为价格 foreach 循环运行某种 SQL 命令,它只会将其添加到顶行并向下移动?

最佳答案

我认为这应该在一个循环中完成:

foreach($typeids as $index => $typeid)
{
$item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
$price= (float) $item[0]->buy->max;
$price=round($price,2);

$query1 = "INSERT INTO data (typeID, Name, Price) VALUES ('$typeid', '$name[$index]', '$price');";

$q1 = mysqli_query($conn,$query1) or die ('Error posting data');
echo $typeids[$index].$name[$index].$price[$index]."\n";
}

关于php - 多个数组插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com