我在下面写了从网页中提取数据,但这种情况持续不断地将数据插入到表中。如何在下面的代码中拆分此转换。
我想立即为每个url插入并循环提交。这不起作用:
<?php
// example of how to use basic selector to retrieve HTML contents
ini_set('log_errors','0');
ini_set('display_errors','1');
error_reporting(2047);
include('simple_html_dom.php');
include('parameters.php');
// get DOM from URL or file
set_time_limit(0);
$site_name="sitename";
mysql_connect($hostname, $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($database);
$query="select site_name,category,subcategory,link,first_no,last_no
from `search_links` where site_name='".$site_name."'";
echo $query;
$res=mysql_query($query);
while ($row = mysql_fetch_assoc($res))
{
$links[]=array(
"site_name"=>$row["site_name"],
"category"=>$row["category"],
"subcategory"=>$row["subcategory"],
"url"=>$row["link"],
"first_no"=>$row["first_no"],
"last_no"=>$row["last_no"]);
}
foreach ($links as $link)
{
for ($i=$link["first_no"];$i<$link["last_no"];$i++)
{
try
{
$html = file_get_html($link["url"].$i);
$sql = array();
foreach($html->find('a') as $e)
{
$sql[] = "('".$e->href."',
'".$site_name."',
'".$link["category"]."',
'".$link["subcategory"]."','N')";
}
#var_dump($sql);
mysql_connect($hostname, $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($database);
$sql_ext=" ON DUPLICATE KEY update duplicate='Y'";
/*//echo('INSERT INTO table (link,site,category,subcategory, archived)
VALUES '.implode(',', $sql));*/
mysql_query(
'INSERT INTO classifieds (link,site,category,subcategory, archived)
VALUES '.implode(',', $sql).$sql_ext);
mysql_query("COMMIT");
}
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
}
}
?>
答案 0 :(得分:1)
mysql扩展不会抛出异常 所以,要么使用常规错误报告
$sql = 'INSERT INTO classifieds (link,site,category,subcategory, archived)
VALUES '.implode(',', $sql).$sql_ext;
mysql_query($sql) or trigger_error(mysql_error().$sql);
或以这种方式抛出异常
if (!mysql_query($sql)) {
throw new Exception(mysql_error().$sql);
}