简体   繁体   English

如何使用simple_html_dom.php将数组插入MySQL数据库?

[英]how do I insert an array into a MySQL database using simple_html_dom.php?

I am trying to insert into a MySQL database the results of these statements. 我试图将这些语句的结果插入到MySQL数据库中。 The daatbase is setup correctly. 数据库已正确设置。 When I insert into a database I get 3 dots "..." rather than what I am suppose to get. 当我插入数据库时​​,我得到3个点“ ...”,而不是我应该得到的。 the last one for tel stores correctly, only the first two do not store into the database but only dots. tel的最后一个正确存储,只有前两个不存储到数据库中,而仅存储点。

for($i = 0; $i < 5; $i++){

    foreach($html->find('span.listado_destacado',$i) as $e){
      if(!empty($e->plaintext)){
        $list[$i] = $e->plaintext;
        echo $list[$i];
      }
    }

    foreach($html->find('span.street-address',$i) as $e){
      if(!empty($e->plaintext)){
        $addr[$i] = $e->plaintext;
        echo $addr[$i];
      }
    }

    foreach($html->find('span.tel',$i) as $e){
      if(!empty($e->plaintext)){
        $tel[$i] = $e->plaintext;
        echo $tel[$i];
      }
    }

    }//for

    for($i = 0; $i < 5; $i++){
      $res=mysql_query("insert into datos (list,addr,tel) values('".$list[$i]."','".$addr[$i]."','".$tel[$i]."')");


    }

There is an error in for loop correct code is as follows: for循环正确代码中存在错误,如下所示:

for($i = 0; $i < 5; $i++){

foreach($html->find('span.listado_destacado',$i) as $e){
  if(!empty($e->plaintext)){
    $list[$i] = $e->plaintext;
    echo $list[$i];
  }
}

foreach($html->find('span.street-address',$i) as $e){
  if(!empty($e->plaintext)){
    $addr[$i] = $e->plaintext;
    echo $addr[$i];
  }
}

foreach($html->find('span.tel',$i) as $e){
  if(!empty($e->plaintext)){
    $tel[$i] = $e->plaintext;
    echo $tel[$i];
  }
}

}//for

for($i = 0; $i < 5; $i++){
  $res=mysql_query("insert into datos (list,addr,tel) values('".$list[$i]."','".$addr[$i]."','".$tel[$i]."')");


}

Give a try to DALMP - multipleinsert or Dalmp - AutoExecute 尝试DALMP-多重 插入Dalmp-自动执行

That can give you an idea of how to insert an array or multiple arrays in one shot. 这样可以使您了解如何在一个镜头中插入一个或多个数组。

Basically you prepare your array and later just insert it to the database. 基本上,您准备阵列,然后将其插入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM