Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:11:54+00:00 2026-05-24T12:11:54+00:00

I am trying to convert my not so well structured mysql article data to

  • 0

I am trying to convert my not so well structured mysql article data to a better many-to-many structure.

basically I have on article table with fields ‘article_id, title, author_1, author_2, author_3, … , author_10’ where article id is unique and the author fields hold full names.

What I need is a manay-to-many relationship where I end up with an article table of ‘article_id, title’ an intermediate table which holds ‘article_id, author_id’ and an author table which holds ‘author_id, author_name’.

I know there must be a way to do this with a few lines of code. I have tried all day with puling the data out and trying to match arrays in php but getting nowhere.

Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:11:55+00:00Added an answer on May 24, 2026 at 12:11 pm

    It’s pretty straightforward:

    • Outer loop selects all article rows from the existing table
    • For each of the author columns, insert a row into the authors table
    • After each insert, use mysql_insert_id() to get the ID of the inserted row, and insert the relationship into the article/author table.

    –

    $sql = "SELECT article_id, title, author_1, author_2, author_3, author_4, author_5, author_6, author_7, author_8, author_9, author_10 FROM articles";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
    
      for ($i = 1; $i <= 10; $i++) {
    
        if (!empty($row['author_' . $i])) {
    
          $author = mysql_real_escape_string($row['author_' . $i]);
    
          //check if author exists
          $check = mysql_query("SELECT author_id FROM authors WHERE author_name = '$author'");
          if (mysql_num_rows($check) > 0) {
            $author_id = mysql_result($check,0); 
          } else {
            mysql_query("INSERT INTO authors (author_name) VALUES ('$author')");
            $author_id = mysql_insert_id();
          }
    
          mysql_query("INSERT INTO article_author (article_id, author_id) VALUES ({$row['article_id']}, $author_id)");
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a two-dimensional array into a structured array with named fields.
I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in
I'm trying to convert the following jquery to javascript syntax, but being not familiar
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
I'm trying to convert a sample from objective C to Monotouch, and I have
I have a MongoMapper model and am trying to convert a comma-delimited string into
I'm trying (with Dozer) to convert a JAXB DTO to a POJO (well, actually,
Well I am trying to convert an image into an button on a page
I'm trying to convert following text to Date but not able to get it
I have a .NETCF project that I have been trying to convert from .NETCF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.