简体   繁体   English

我无法使用Simple_html_dom.php将数组存储到MySQL数据库中

[英]I can't store array into MySQL database, Im using simple_html_dom.php

What I want to do is store the value that is not empty into the database, my database connection is correct, the problem here is that when I store into the database only the Id increments and that is because the Id is set on auto-increment. 我想做的是将不为空的值存储到数据库中,我的数据库连接正确,这里的问题是,当我将ID存储到数据库中时,仅Id递增,这是因为Id设置为自动递增。

this is the code that I have: 这是我的代码:

include("connect.php");
mysql_select_db("scrapper",$conec);

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

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

inside the foreach there are multiple blank strings "" and those are what im trying to avoid thats why I only want to store the plaintext when empty is negated, but I still keep getting nothing inside the database only two periods which are inside the '".$list[$i]."' . 在foreach中有多个空字符串“”,这就是我要避免的原因,这就是为什么我只想在取空为空时才存储纯文本,但是在数据库中仍然只有两个句点在'".$list[$i]."'内,我什么也没得到'".$list[$i]."' Also the echo above does work echo $list[$i]; 同样,上面的回显确实起作用echo $list[$i]; It prints out what I want it to store in the database. 它打印出我想要存储在数据库中的内容。

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

      $res=mysql_query("insert into data (list) values('".$list[$i]."')");


}

here is more code: 这是更多代码:

$url = " http://www.seccionamarilla.com.mx/resultados/hospitales/distrito-federal/ "; $ url =“ http://www.seccionamarilla.com.mx/resultados/hospitales/distrito-federal/ ”; $html = file_get_html($url); $ html = file_get_html($ url);

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

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

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

echo '<br>';

}//end for

for($i = $0; $i < $5; $i++){
  $res=mysql_query("insert into data (list,addr) values('{$list}','{$addr}')");


}

This Is what I get after I run the program: 这是我运行程序后得到的:

ANESTESIA Y CONTROL DEL DOLOR MEDICOS ASOCIADOS SC RIO CHURUBUSCO 601 421, XOCO, BENITO JUAREZ, C.P 03330, DF 
CENTRO HOSPITALARIO SANATORIO DURANGO DURANGO 296, ROMA, CUAUHTEMOC, C.P 06700, DF 
CLISEM NICOLAS SAN JUAN 351, DEL VALLE CENTRO, BENITO JUAREZ, C.P 03100, DF 
HOSPITAL MEDICA SAN LUIS SAN LUIS POTOSI 122, ROMA NORTE, CUAUHTEMOC, C.P 06700, DF 
MEDICA PALMAS LAS PALMAS 10, GRANJAS CABRERA, TLAHUAC, C.P 13230, DF 

Try this(remove foreach loops): 试试这个(删除foreach循环):

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


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


echo '<br>';

}//end for

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

http://simplehtmldom.sourceforge.net/manual.htm http://simplehtmldom.sourceforge.net/manual.htm

Try this. 尝试这个。

for($i = $0; $i < $3; $i++){    
    $data = $list[$i];
    $res=mysql_query("insert into data (list) values('{$data}')");
}

In response to your comment: Maybe I misunderstood. 回应您的评论:也许我误会了。 Are the two code snippets above separate for loops in your program? 上面的两个代码段是否在程序中的for循环中分开? Or are you trying to merge them into one? 还是您想将它们合并为一个?

You could do what your asking simply by replacing your echo statement in the first code block with the insert query. 您只需将第一个代码块中的echo语句替换为insert查询即可完成您的要求。

Im also unsure as to why youre storing your data into an array like that if youre just inserting it into a database. 如果您只是将数据插入数据库,我还不确定为什么要将数据存储到数组中。

Example: 例:

include("connect.php");
mysql_select_db("scrapper",$conec);

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

    foreach($html->find('span.lists',$i) as $e){
        if(!empty($e->plaintext)){
            $data = $e->plaintext;
            $res=mysql_query("insert into data (list) values('{$data}')");
        }        
    }
}

CS2032 C PASS CS2041 D PASS CS2401 C PASS CS2402 C PASS CS2403 E PASS CS2405 S PASS CS2406 S PASS MG2452 B PASS i have plaintext got from inside foreach i need to get set of three values like CS2032 C PASS CS2041 D PASS CS2401 C PASS CS2402 C PASS CS2403 E PASS CS2405 S PASS CS2406 S PASS MG2452 B PASS我从内部获取了纯文本foreach我需要获取三个值的集合

CS2032 C PASS CS2041 D PASS CS2032 C合格证CS2041 D合格证

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

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