如何避免在 MySQL 中插入重复行?
How can I keep from inserting duplicate rows in MySQL?
我一直在尝试将 RSS 文章保存在我的数据库中(来自 RSS URL)并且它正在运行。但是现在我想防止插入重复的文章(行)并且我尝试了下面的编码但它不起作用。每次运行时,它似乎都工作正常,除了它插入所有文章并且不会停止复制(文章存在新记录的 ID 为 144)。为什么会这样?
<?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();
}
?>
您的插入查询在 if($checkrows>0)
条件的 true 分支中。将其移动到 false 分支,在 else 下。
您可以将唯一键添加到您的文章 ID 列并使用 INSERT IGNORE 语句
我一直在尝试将 RSS 文章保存在我的数据库中(来自 RSS URL)并且它正在运行。但是现在我想防止插入重复的文章(行)并且我尝试了下面的编码但它不起作用。每次运行时,它似乎都工作正常,除了它插入所有文章并且不会停止复制(文章存在新记录的 ID 为 144)。为什么会这样?
<?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();
}
?>
您的插入查询在 if($checkrows>0)
条件的 true 分支中。将其移动到 false 分支,在 else 下。
您可以将唯一键添加到您的文章 ID 列并使用 INSERT IGNORE 语句