Php 链接与mysql数据库中的其他数据分开保存
我正在尝试将网站中的数据保存到mysql数据库中。我能够保存大部分我想保存的东西,但我有一个特别的问题。我提取的链接正在保存,但我希望链接与其他属性的行相同Php 链接与mysql数据库中的其他数据分开保存,php,mysql,curl,Php,Mysql,Curl,我正在尝试将网站中的数据保存到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 Do
$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
-- ----------------------------
$target\u url=”http://www.ucc.ie/modules/descriptions/BM.html";
$code=“BM”;
$html=文件\u获取\u内容(“http://www.ucc.ie/modules/descriptions/BM.html");
@$doc=新的DomDocument();
@$doc->loadHtml($html);
//丢弃空白
@$doc->preserveWhiteSpace=false;
$xpath=新的DomXPath($doc);
//通读dd标签
$options=$doc->getElementsByTagName('dd');
//进入dd标签并查找modnav类的所有链接
$links=$xpath->query('//dd//a[@class=“modnav”]');
//循环浏览并显示链接的结果
foreach($links作为$link){
echo$link->getAttribute('href'),“
”;
}
foreach($options作为$option){
$option->nodeValue;
echo“节点值(模块名称/标题)=$option->nodeValue
;
//将每个结果的两个结果保存到数据库中
$query3=sprintf(“插入所有模块名称(代码、模块名称、描述链接、收集自))
值('%s','%s','%s','%s','%s')”,
mysql\u real\u escape\u字符串($code),
mysql\u real\u escape\u字符串($option->nodeValue),
mysql\u real\u escape\u字符串($link->getAttribute('href'),
mysql_real_escape_字符串($target_url));
mysql_query($query3)或die(mysql_error()。“
”。$query3);
}
回声“
”;
这是桌子
-- ----------------------------
--“所有模块名称”的表结构`
-- ----------------------------
如果存在“所有模块名称”,则删除表;
创建表“所有模块名称”和“副本”(
`代码'varchar(255)不为空,
`模块名称'varchar(255)不为空,
`description_link`varchar(255)不为空,
`从'varchar(255)收集的_不为空
)ENGINE=MyISAM默认字符集=1;
-- ----------------------------
--所有模块名称的记录
-- ----------------------------
所以问题是“$link->getAttribute('href')”与我试图保存的其他内容分开保存。首先保存链接,然后保存其余数据,留下一些行为空,但我试图一次保存所有内容,即填充每一行,然后移到第二行,直到for each语句完成。请问我怎么做?任何帮助都将不胜感激 未经测试(因此需要调试),但我会这样做:
...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 ...
查询应该在一个循环中,还有一个额外的美元符号
mysql\u real\u escape\u字符串($$link->getAttribute('href'),
,这似乎不是有意的。这很可能会导致该字段为空。嘿,谢谢,我在循环中有查询,但仍然得到相同的结果@Sean Johnson和我纠正了$$的错误,但仍然得到了相同的结果。嗨Robbie,谢谢你的修改。我尝试使用上面的代码,但我得到了错误:解析错误:语法错误,意外'。此错误与此行相关:$aClasses=explode(“”,)代码>。有解决这个问题的建议吗?修正了那行。您可能会遇到其他错误,还需要调试-如果您这样做了,很抱歉,但这只是一个未经测试的建议(基于没有其他人回答的事实)。注意:我还编辑了行$href=$link->getAttribute('href');因为这是错误的嘿Robbie,非常感谢你修复了错误。我还修复了foreach语句。这两个变量的名称相同,并且在代码末尾添加了另一个结束括号。非常感谢你的时间和努力,我会尝试一下,让你知道我是如何处理的。谢谢