2012-06-25 3 views
2

웹 사이트의 데이터를 mysql 데이터베이스에 저장하려고합니다. 나는 내가 구하고 싶어했던 것의 대부분을 구할 수 있었지만 특별한 문제가있다. 내가 압축을 푼 링크는 저장하고 있지만 링크를 다른 속성과 동일한 행에 넣고 싶습니다. 아래는 데이터베이스에 정보를 추출하고 저장하는 내 CURL 및 mysql 쿼리입니다.mysql 데이터베이스의 다른 데이터와 별도로 링크 저장

$target_url = "http://www.ucc.ie/modules/descriptions/BM.html"; 
$codeS = "BM"; 
$html = file_get_contents("http://www.ucc.ie/modules/descriptions/BM.html"); 
@$doc = new DomDocument(); 
@$doc->loadHtml($html); 
//discard white space 
@$doc->preserveWhiteSpace = false; 
$xpath = new DomXPath($doc); 

//Read through dd tags 
$options = $doc->getElementsByTagName('dd'); 

//Go into dd tags and look for all the links with class modnav 
$links = $xpath->query('//dd //a[@class = "modnav"]'); 

//Loop through and display the results for links 
foreach($links as $link){  
echo $link->getAttribute('href'), '<br><br>'; 
} 

foreach ($options as $option) { 

    $option->nodeValue; 
    echo "Node Value (Module name/title)= $option->nodeValue <br /><br /> <br />"; 

     // save both for each results into database 
$query3 = sprintf("INSERT INTO all_modulenames(code,module_name,description_link,gathered_from) 
    VALUES ('%s','%s','%s','%s')", 
    mysql_real_escape_string ($codeS), 
    mysql_real_escape_string($option->nodeValue), 
    mysql_real_escape_string($link->getAttribute('href')), 
    mysql_real_escape_string($target_url)); 
    mysql_query($query3) or die(mysql_error()."<br />".$query3); 

    } 
    echo "<br /> <br /> <br />"; 


Here is the table 
-- ---------------------------- 
-- Table structure for `all_modulenames` 
-- ---------------------------- 
DROP TABLE IF EXISTS `all_modulenames`; 
CREATE TABLE `all_modulenames_copy` (
`code` varchar(255) NOT NULL, 
`module_name` varchar(255) NOT NULL, 
`description_link` varchar(255) NOT NULL, 
`gathered_from` varchar(255) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of all_modulenames 
-- ---------------------------- 

그래서 문제는 떠나고 나머지 데이터에 의해 처음 저장 한 후 준수 save.The 링크에 노력하고 별도로 다른 콘텐츠에서 저장되어있는 "$ 링크 ->의 getAttribute ('HREF')"입니다 일부 행은 비어 있지만 모든 것을 모두 저장하려고합니다. 즉 각 행을 채우고 각 명령문이 끝날 때까지 두 번째 행으로 이동합니다. 내가 어떻게 이럴 수 있니? 어떤 도움을 주시면 감사하겠습니다 !!

+0

조회가 루프 –

+0

($$ 링크 ->의 getAttribute는 mysql_real_escape_string 여분의 달러 기호 '('또한 거기에 내부되어야한다 href ')), 의도적으로 보이지는 않습니다. 이 경우 해당 입력란이 비어있을 가능성이 큽니다. –

+0

안녕하세요, 루프의 쿼리가 있는데 여전히 동일한 결과가 나타납니다. @Sean Johnson과 나는 $$의 실수를 수정하고 여전히 같은 결과를 얻고 있습니다. – user1444442

답변

1

테스트되지 않은 (그래서 디버깅해야합니다)하지만이 같은 접근 것 :

...etc 
@$doc->preserveWhiteSpace = false; 

//Read through dd tags 
$options = $doc->getElementsByTagName('dd'); 

foreach ($options as $option) { 

    // Get the links and find the one with the right class 
    $href = ''; 
    $links = $option->getElementsByTagName('a'); 
    foreach ($link as $link) { 
     if ($link->hasAttribute('class') && $link->hasAttribute('href')) { 
      $aClasses = explode(' ', $link->getAttribute('class')); 
      if (in_array('modnav', $aClasses)) { 
        $href=$link->getAttribute('href'); 
      } 
     } 
    } 

    Insert in to SQL etc, $href is the link text belonging to the dd ... 
+0

안녕하세요 로비, repply 주셔서 감사합니다. 위의 코드를 사용했지만 오류가 있습니다 : ** 구문 분석 오류 : 예기치 않은 구문 오류 ') **. 이 에러는 다음 줄에 적용된다 :'$ aClasses = explode ('',);'이 문제를 해결하기위한 제안은 없습니까? – user1444442

+0

해당 행이 수정되었습니다. 다른 오류가 발생하고 디버그해야 할 수도 있습니다. 미안하지만 테스트를 거친 제안 일뿐입니다 (다른 사람이 대답하지 않는 사실에 기반). 참고 : 또한 $ href = $ link-> getAttribute ('href') 행을 편집했습니다. 그게 틀렸어 – Robbie

+0

안녕하세요 Robbie, 오류를 해결하기위한 많은 감사합니다. 나는 또한 foreach 문을 해결했습니다 .2 변수는 같은 이름을 가지고 또한 코드의 끝에 다른 닫는 괄호를 추가했습니다. 정말 시간과 노력을 주셔서 감사합니다, 나는 그것을 가지고 가서 어떻게 지내는지 알려줄거야. 고마워. – user1444442

관련 문제