简体   繁体   中英

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. The daatbase is setup correctly. When I insert into a database I get 3 dots "..." rather than what I am suppose to get. the last one for tel stores correctly, only the first two do not store into the database but only dots.

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($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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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