XML Feed更新MySQL数据库

时间:2018-04-01 01:21:50

标签: php mysql xml

$time=date("Y-m-d H:i:s"); 

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/feed.xml");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned=curl_exec($ch);
curl_close($ch);

$xml=simplexml_load_string($returned);

if (!$xml=="") {
foreach($xml as $lists){

$con=mysqli_connect('localhost','username','password','database') or die();
$result=mysqli_query($con,"SELECT count(*) as numrecords FROM db_1 WHERE username='".$lists->username."' LIMIT 1") or die (); 
$row=mysqli_fetch_assoc($result);

if ($row['numrecords'] >= 1)
{  
mysqli_query($con,"UPDATE db_1 SET time='".$time."', subject='".$lists->subject."', views='".$lists->views."' WHERE username='".$lists->username."' LIMIT 1"); 
}
else 
{
$entry="INSERT INTO `db_1` (`id`, `time`, `username`, `subject`, `views`) VALUES ('', '".$time."', '".$lists->username."', '".$lists->subject."', '".$lists->views."');";
mysqli_query($con, $entry);
}
mysqli_close($con);

} }
exit;

您好,

脚本每x分钟通过一个cronjob调用。

代码将在几天内完美运行,然后会随机乱搞并随时更改&受试者在同一时间/受试者即使在记录上它也不应该更新..

有人有什么想法吗?

由于

0 个答案:

没有答案