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 8392153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:26:48+00:00 2026-06-09T19:26:48+00:00

Hoping for some assistance here. I have a large xml file (called xml-file.xml in

  • 0

Hoping for some assistance here. I have a large xml file (called xml-file.xml in the code above). The problem I am having with the code above is that it is very slow to write everything to the database. I think the reason it is slow is because of the section that loops through the movies and writes only the movie name in a comma separated string to the table. My question – is there a more efficient way to convert the movie names to a comma separated string and then write to the table? Thanks in advance for any help!

The XML Markup:

    <users>
      <user>
        <id>7280462</id>
        <name>John</name>
        <movies>
          <name>
            <![CDATA[Jack]]>
          </name>
          <description>
            <![CDATA[comedy]]>
          </description>
          <name>
            <![CDATA[Superman]]>
          </name>
          <description>
            <![CDATA[action]]>
          </description>
          <name>
            <![CDATA[Ace Venture]]>
          </name>
          <description>
            <![CDATA[comedy]]>
          </description>
          <name>
            <![CDATA[Major League]]>
          </name>
          <description>
            <![CDATA[sports]]>
          </description>
        </movies>
      </user>
    </users>

My PHP code:

    $file = 'xml-file.xml';
    $users = simplexml_load_file($file);

    $num_rows = count($users);
    for ($j=0; $j < $num_rows; $j++) { 
        $userid = $users->user[$j]->id;
        $name = $users->user[$j]->name;

        //update table with userid and name
        $db->exec("INSERT INTO table (userid, name) VALUES ('$userid', '$name')");

            //loop through movies and write only movie name to table as comma separated string
            foreach ($users->user$j]->movies as $element) {
              foreach($element as $key => $val) {
                  if ($key != 'description') {
                       //echo "{$key}: {$val}";
                       $title = trim($val).',';
                      $db->exec("UPDATE table set movies = concat(movies,'','$title') where userid = '$userid'");
                    }
              }
            }

    }
  • 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-06-09T19:26:49+00:00Added an answer on June 9, 2026 at 7:26 pm

    How about this:

    for ($j=0; $j < $num_rows; $j++) { 
        $userid = $users->user[$j]->id;
        $name = $users->user[$j]->name;
        $titles = "";
    
        foreach ($users->user$j]->movies as $element) {
            foreach($element as $key => $val) {
                if ($key != 'description') {
                       $titles .= trim($val).',';
                }
            }
        }
    
        $titles = trim($titles, ",");
    
        $db->exec("INSERT INTO table (userid, name, movies) VALUES ('$userid', '$name','$titles')");
    
     }
    

    It’s generating all the SQL in one go, so you just need a single statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I'm hoping to get some direction in solving -
I'm hoping people have some ideas to help solve this problem. I am developing
I am having real trouble trying to deserialize some XML and was hoping someone
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin
I am having troubles with jQuery's load function and am hoping for some help.
I'm hoping there are some fellow doctrine users out there. Here is a simplified
I was hoping to get some advice on what to do. I have a
A long shot but here's hoping someone has some experience coding PHP hooks for
I am hoping some smart folks can help me here. I'm out of my
Hoping some could direct me to a tool that i can use to monitor

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.