链接保存与mysql数据库中的其他数据分开

问题描述:

我想将网站中的数据保存到mysql数据库中。我能够挽救大部分我想要拯救的东西,但我有一个特殊的问题。我提取的链接是保存,但我想链接在相同的行其他attributes.Below是我的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 
-- ---------------------------- 

所以,问题是“$链路>的getAttribute(‘href’属性)”分别从其他内容保存在尝试save.The链接保存,然后再接着是数据的保留其余有些行是空的,但我试图一次保存所有内容,即填充每行,然后移动到第二行,直到每个语句完成。我怎么能这样做?任何帮助,将不胜感激 !!

+0

查询应该是一个循环 – 2012-06-25 00:43:46

+0

这里还有一个额外的美元符号'mysql_real_escape_string($$链路>的getAttribute(”内href')),'这似乎不是故意的。这很可能会导致该字段为空。 –

+0

嘿谢谢我在循环中查询,我仍然得到同样的结果。 @肖恩约翰逊和我纠正了$$的错误,但仍然得到相同的结果。 – user1444442

未经测试的(所以需要调试),但我想办法是这样的:

...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

嗨Robbie,感谢您的repply.I尝试使用上面的代码,但我得到了错误:**解析错误:语法错误,意外的')'**。这个错误实现了这一行:'$ aClasses = explode('',);'。有没有解决这个问题的建议? – user1444442

+0

修复该行。你可能会得到其他错误,也需要调试 - 对不起,如果你这样做,但它只是一个未经测试的建议(基于其他人无法回答的事实)。注意:我也编辑过$ href = $ link-> getAttribute('href');因为这是错的 – Robbie

+0

嗨罗比,非常感谢修复这个错误。我也修正了foreach语句。这两个变量有相同的名字,并且在代码的末尾增加了另一个右括号。非常感谢你的时间和精力,我会给它一个去,让你知道我怎么得到它。感谢 – user1444442