私はRSS記事をRSS URLからデータベースに保存しようとしています。しかし今は重複記事(行)を挿入しないようにしたいのですが、以下のコードを試しましたが動作しません。それが実行されるたびに、それはすべての記事を挿入し、複製を停止しない限り、すべて正常に動作しているように見えます(新しいレコードはID 144を持っています)。なぜこうなった?MySQLで重複行を挿入しないようにするにはどうすればよいですか?
<?php
$doc = new DOMDocument();
$doc->load('yoursite.com/rss.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
);
array_push($arrFeeds, $itemRSS);
}
$mysqli = new mysqli('localhost', '', '', 'testdb');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$check=mysqli_query($mysqli,"select * from `articles` where `title`=`title` and `description`=`description`");
$checkrows=mysqli_num_rows($check);if (!$check) { die('Invalid query: ' . mysql_error());}
if($checkrows>0) {
echo "article exists";
if ($stmt = $mysqli->prepare("INSERT INTO `articles` (`title`, `description`, `link`) VALUES (?, ?, ?)")) {
$stmt->bind_param('sss', $title, $description, $link);}
else {die("Errormessage: ". $mysqli->error);}
foreach($arrFeeds as $RssItem){
$title = $RssItem["title"];
$description = $RssItem["description"];
$link = $RssItem["link"];
$stmt->execute();
}
printf ("New Record has id %d.\n", $mysqli->insert_id);
$stmt->close();
$mysqli->close();
}
?>
を使用することができますが、それが今でソートされ、あなたの助けをいただき、ありがとうございます。 – ika