gpt4 book ai didi

mysql - 在循环中将数组值更多列插入到mysql

转载 作者:行者123 更新时间:2023-11-29 07:36:57 26 4
gpt4 key购买 nike

我使用 SimpleHtmlDom 生成了结果。

include('simple_html_dom.php');
$html = file_get_html('http://www.example.com');

$count =count($html->find('table tbody tr td'))-1; //$count =170

for ($i=1; $i < $count; $i++) {
echo $val[$i] =$html->find('table tbody tr td',$i)->plaintext;
}

我不需要第一个和最后一个输出,因此使用了$x=1 和 $count-1

输出结果为

enter image description here

这里,每一行的股票代码都是相同的,并且每第 8 个数组值后面有一个循环。

我的 mysql 表是记录,其中列为“id,date,stock_symbol,buyer,seller,quantity,rate,amount

"INSERT INTO records 
(id,date,stock_symbol,buyer,seller,quantity,rate,amount)
VALUE ('$val1','$val2','$val3','$val4','$val5','$val6','$val7','$val8')",
('$val9','$val10','$val11','$val12','$val13','$val14','$val15','$val16')"

如果数量少的话我可以手动完成,但它超过了几百个。

我试过了

$values = array('English', 'Arabic');
$statement = 'INSERT INTO contact (`language1`, `language2`) VALUES ("' . implode('", "', $values) . '")';
echo $statement;

来自 How insert the array value one by one to MySQL table in different column ,但我的数组没有逗号(,)并且没有得到任何结果

编辑:在 ans 之后编辑

include('inc/simple_html_dom.php');
include('mysql.php');
// get DOM from URL or file

header('Content-Type: text/html; charset=utf-8');
// get DOM from URL or file
$html = file_get_html('http://www.seismonepal.gov.np/index.php?action=earthquakes&show=recent');

// remove all image
foreach($html->find('div [class=block2-content] span') as $e)
$array= str_replace(' ','',$e->plaintext);

//split all you data into the size chunks you want.
//$array = array('English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French');
$chunks = array_chunk($array, 7);

//get the amount of chunks you have.
$c = count($chunks);
$a = 0;

//then run through each chunk entering it into the database.

do{
$values = null;
$x = 1;

foreach($chunks[$a] as $value) {
$values .= "?";
if($x < count($chunks[$a])) {
$values .= ', ';
}

$x++;
}

$sql = "INSERT INTO earthquake(id,date_np,local_time,lattitude,longitude,magnitude,epic_center,remarks) VALUES ({$values})";
$query = $db->prepare($sql);

if(count($chunks[$a])){
$y = 1;
foreach($chunks[$a] as $param) {
$query->bindValue($y, $param);
$y++;
}
}


if($query->execute()){
echo "Done {$a}" . "<br/>";
}else{
echo "Not Done {$a}" . "<br/>";
}
$a++;

}while($a < $c);

最佳答案

这将进行多个查询,但这是您可以做到的一种方式。

将所有数据分割成您想要的大小块。

$array = array('English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French');
$chunks = array_chunk($array, 2);

获取您拥有的 block 数量。

$c = count($chunks);
$a = 0;

然后运行每个 block 并将其输入数据库。

do{
$values = null;
$x = 1;

foreach($chunks[$a] as $value) {
$values .= "?";
if($x < count($chunks[$a])) {
$values .= ', ';
}

$x++;
}

$sql = "INSERT INTO `table` (`language1`, `language2`) VALUES ({$values})";
$query = $db->prepare($sql);

if(count($chunks[$a])){
$y = 1;
foreach($chunks[$a] as $param) {
$query->bindValue($y, $param);
$y++;
}
}


if($query->execute()){
echo "Done {$a}" . "<br/>";
}else{
echo "Not Done {$a}" . "<br/>";
}
$a++;

}while($a < $c);

希望这有帮助。

编辑后续问题。

当你找到它们时,你可以将它们放入一个数组中,如下所示。

$array = array();

foreach($html->find('div [class=block2-content] span') as $e){
array_push($array, $e->plaintext);
}

然后您可以使用array_chunk

关于mysql - 在循环中将数组值更多列插入到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427425/

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