dsiv4041 2012-02-28 13:10
浏览 64
已采纳

在mysql中插入一个值数组

this is an edited code in which mr. Paulo Rodrigues, php curl is used to get the entries or data in the specified webpage. it will save in the file.txt. Open the file then gets the data of each entry then loop in order for the all data required in the entries is inserted.

$ch = curl_init("http://www.uniprot.org/uniprot/?query=(annotation%3a(type%3asignal+confidence%3aexperimental))+AND+reviewed%3ayes&sort=score&limit=10&format=txt");
$fp = fopen("file.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);


while (!feof($fp))
{

$array = array();
$index = -1;

foreach ($fp as $line) {
if (preg_match('#^ID#', $line)) {
    $index++;
}

$array[$index][] = $line;
}

foreach ($array as $block) {
$fields = array();

foreach ($block as $item) {
    if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
        print_r($fields['prot_name'] = $matches[1]);
        echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    } 
   else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
       print_r($fields['tax_lin'] = $matches[1]);
         echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
             &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    } 
    else if (preg_match('#LOCATION:\sMembrane;\s(.*?)\s#', $item, $matches)) {
        print_r($fields['sub_loc'] = $matches[1]); 

    } 

    }

    echo "<br />";

    if (!empty($fields)) {
    $f = implode(', ', array_keys($fields));
    $v = "'" . implode("', '", $fields) . "'";

    $sql = "INSERT INTO swissprot ($f) VALUES($v)";
    mysql_query($sql) or die(mysql_error());
    }


   }
 }

curl_close($ch);
fclose($fp);

the problem is that there is a warning that displays: "Warning: Invalid argument supplied for foreach() in this line. what might be the problem?

foreach ($fp as $line) {
if (preg_match('#^ID#', $line)) {
    $index++;
}

$array[$index][] = $line;
}
  • 写回答

1条回答 默认 最新

  • duankun9280 2012-02-28 13:29
    关注

    You could try this:

    $webpagedata = file('http://www.uniprot.org/uniprot/?query=annotation%3a%28type%3asignal+confidence%3aexperimental%29+reviewed%3ayes&sort=score&limit=10&format=txt');
    
    $array = array();
    $index = -1;
    
    foreach ($webpagedata as $line) {
        if (preg_match('#^ID#', $line)) {
            $index++;
        }
    
        $array[$index][] = $line;
    }
    
    foreach ($array as $block) {
        $fields = array();
    
        foreach ($block as $item) {
            if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
                $fields['prot_name'] = $matches[1];
            } else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
                $fields['tax_lin'] = $matches[1];
            } else if (preg_match('#LOCATION:\sMembrane;\s(Single-pass)#', $item, $matches)) {
                $fields['sub_loc'] = $matches[1];
            }
        }
    
        if (!empty($fields)) {
            $f = implode(', ', array_keys($fields));
            $v = "'" . implode("', '", $fields) . "'";
    
            $sql = "INSERT INTO spdb ($f) VALUES($v)";
            mysql_query($sql) or die(mysql_error());
        }
    }
    

    Update:

    $ch = curl_init("http://www.uniprot.org/uniprot/?query=(annotation%3a(type%3asignal+confidence%3aexperimental))+AND+reviewed%3ayes&sort=score&limit=10&format=txt");
    $fp = fopen("file.txt", "w");
    
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    
    $file = file("file.txt");
    $array = array();
    $index = -1;
    
    foreach ($file as $line) {
        if (preg_match('#^ID#', $line)) {
            $index++;
        }
    
        $array[$index][] = $line;
    }
    
    foreach ($array as $block) {
        $fields = array();
    
        foreach ($block as $item) {
            if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
                print_r($fields['prot_name'] = $matches[1]);
                echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            } else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
                print_r($fields['tax_lin'] = $matches[1]);
                echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            } else if (preg_match('#LOCATION:\sMembrane;\s(.*?)\s#', $item, $matches)) {
                print_r($fields['sub_loc'] = $matches[1]);
            }
        }
    
        echo "<br />";
    
        if (!empty($fields)) {
            $f = implode(', ', array_keys($fields));
            $v = "'" . implode("', '", $fields) . "'";
    
            $sql = "INSERT INTO swissprot ($f) VALUES($v)";
            mysql_query($sql) or die(mysql_error());
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?