简体   繁体   中英

Converting Array values into a temporary table

I have two columns member id and author id

Member A gives a list of author ids in a array format. I need to run a check against my database to see if these author ids are already there in my database, if they are present take the member ids of these authors and link them to the member id of A.

What is the efficient method to do this? Is it advisable to store the author ids to a temporary table and run the check and fetch it. The number of author ids in the array runs to few thousands.

Can you share some sample codes please? I am currently using PHP and MySql.

Update - I get an array of JSON objects with author id and name as follows:

  {
     "name": "Harold Robinson",
     "id": "912065"
  },
  {
     "name": "Gilbert Patten",
     "id": "1212140"
  },
  {
     "name": "Leo Tolstoy",
     "id": "6012954"
  }

I already have a table called Authors against which these data needs to be compared and if the ids match the user id assigned to the author has to be linked to User A.

Table Authors User id: 109876 id: 912065

FYI, you won't get a useful answer if your question is not clear and with enough detail. This is a though one because the question is not very clear but I will attempt to the best of my understanding of the question. We can then tweak the answer as you clarify the question further.

    //Not sure how you are collecting the ids into an array so I will just create one here for show.
$member_authors = array (1, 2, 3);
foreach ($member_authors as $authorID) {
    $query = "SELECT author_id FROM authors WHERE author_id ='" . $authorID . "'";
    $result = mysql_query($query);

    if (mysql_affected_rows() > 0) { //If a value was returned then the author does exist in the database. 
        //Do whatever you want with the authorID.
    }
}

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM