将关联的PHP数组存储到数据库中而不会重复

时间:2022-09-26 08:22:06

I am trying to store an associative PHP array (a table) into the database.This a big array that contains many fields (around 30 fields) and I just want to store 6 fields of this array in to a table which has 6 fields as well. Each row on this array contains the information of a specific article. I want to make sure that each article will be stored only one time (no duplication) so before storing data I need to check database for repetitive queries. Here is my code which is not working properly. I would appreciate if someone help me with this.

我试图将一个关联的PHP数组(一个表)存储到数据库中。这是一个包含许多字段(大约30个字段)的大数组,我只想将这个数组的6个字段存储到一个包含6个字段的表中好。此数组上的每一行都包含特定文章的信息。我想确保每篇文章只存储一次(没有重复),所以在存储数据之前我需要检查数据库是否有重复查询。这是我的代码无法正常工作。如果有人帮我这个,我将不胜感激。

<?php $results = $PubMedAPI->query($term, false); ?>
<?php if (!empty($results)): ?>
<?php
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    foreach ($results as $result):

        $pmid = $result['pmid'];
        $title = $result['title'];
        $authors = $result['authors'];
        $journalabbrev = $result['journalabbrev'];
        $year = $result['year'];
        $abstract = $result['abstract'];
        $fetched_articles = mysqli_query($con,"SELECT pmid FROM articles");

        while( ($row = mysqli_fetch_array($fetched_articles))) {
            if ($row['pmid'] == $pmid) {
                echo "This record has already been stored into the database!";
            } else {
                mysqli_query($con,"INSERT INTO articles (pmid, title, authors, journalabbrev, year, abstract)
                VALUES ('$pmid', '$title', '$authors', '$journalabbrev', '$year', '$abstract')");
                echo "This record has been stored into the database!";
            }
        }
    endforeach;
    mysqli_close($con);
?>
<?php endif; ?>

1 个解决方案

#1


0  

Here's some untested code to do it faster:

这是一些未经测试的代码,可以更快地完成:

$select = "SELECT pmid FROM articles WHERE pmid IN (";
foreach($result in $results) {
    $select .= $result['pmid'] . ',';
}
$select = trim($select,',') . ")";


$existing_pmid = array();
while( ($row = mysqli_fetch_array($select))) {
    $existing_pmid[$row['pmid']] = 1;
}

foreach ($results as $result) {
    if(isset($existing_pmid[$result['pmid']])) {
        echo "This record has already been stored into the database!";
        continue;
    }

    $pmid = $result['pmid'];
    $title = $result['title'];
    $authors = $result['authors'];
    $journalabbrev = $result['journalabbrev'];
    $year = $result['year'];
    $abstract = $result['abstract'];

    mysqli_query($con,"INSERT INTO articles (pmid, title, authors, journalabbrev, year, abstract) VALUES ('$pmid', '$title', '$authors', '$journalabbrev', '$year', '$abstract')");
    echo "This record has been stored into the database!";
}

#1


0  

Here's some untested code to do it faster:

这是一些未经测试的代码,可以更快地完成:

$select = "SELECT pmid FROM articles WHERE pmid IN (";
foreach($result in $results) {
    $select .= $result['pmid'] . ',';
}
$select = trim($select,',') . ")";


$existing_pmid = array();
while( ($row = mysqli_fetch_array($select))) {
    $existing_pmid[$row['pmid']] = 1;
}

foreach ($results as $result) {
    if(isset($existing_pmid[$result['pmid']])) {
        echo "This record has already been stored into the database!";
        continue;
    }

    $pmid = $result['pmid'];
    $title = $result['title'];
    $authors = $result['authors'];
    $journalabbrev = $result['journalabbrev'];
    $year = $result['year'];
    $abstract = $result['abstract'];

    mysqli_query($con,"INSERT INTO articles (pmid, title, authors, journalabbrev, year, abstract) VALUES ('$pmid', '$title', '$authors', '$journalabbrev', '$year', '$abstract')");
    echo "This record has been stored into the database!";
}