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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:23:18+00:00 2026-05-26T20:23:18+00:00

I have a SQL database and i am performing the following query on it.

  • 0

I have a SQL database and i am performing the following query on it.

        $statistics="SELECT firstname AS 'user', count( * ) AS 'num',member_id AS ID
        FROM members
        JOIN member_photo
        USING ( member_id )
        GROUP BY firstname ";
        $result= mysql_query($statistics);

This will retrieve the uploaders and the number of photos they have uploaded in the following format.

        Uploader  Number of photos uploaded
          mun             20
          ris             10
          moz              5

What i want is for a PHP script to create the XML file. It has to actually save it in my directory. I used SimpleXML to do that.

This is what i did..

    $xmlDoc=loadXMLDoc("photo.xml");

        $statistics="SELECT firstname AS 'user', count( * ) AS 'num',member_id AS ID
        FROM members
        JOIN member_photo
        USING ( member_id )
        GROUP BY firstname ";
        $result= mysql_query($statistics);


    while($row=mysql_fetch_assoc($result)) {

    $uploader=$xml->addChild('uploader');
    $uploader->addAttribute('ID',$row['ID']);
    $uploader->addChild('Name', $row['user']);
    $uploader->addChild('Photos_Uploaded', $row['num']);
    $xml->asXML('photo.xml');

    }

where photo.xml is the file the XML nodes are being saved to. They are in this format.

      <?xml version="1.0"?>

      <?xml-stylesheet type="text/xsl" href="user.xsl"?>


      <users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="users.xsd">




       <uploader ID="11"><Name>moz</Name><Photos_Uploaded>2</Photos_Uploaded>             </uploader>

       <uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>

      <uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>

      </users>

Now suppose user “Moz” uploads a new photo. His photo counter is initially 2 which should change to three. The XML file should reflect this change right? It does since it’s pulling data from the database (remember the query?) but here’s the problem.

      <uploader ID="11"><Name>moz</Name><Photos_Uploaded>2</Photos_Uploaded>             </uploader>

       <uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>

      <uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>

moz3

       <uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>

      <uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>

It replicates the other nodes too which creates duplication into the XML file.

What i thought of was to

  1. first clear the file before populating the XML file again.

Anybody knows how to do that? with php or javascript?

  • 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-26T20:23:18+00:00Added an answer on May 26, 2026 at 8:23 pm

    So you’re asking how to ’empty’ the photo.xml file before you write/rewrite it? Is that correct?

    I’m not familiar with SimpleXML, so I don’t know if there is a flag you can use for it or not, but you could always use:

    $f = fopen("photo.xml", "w");
    fclose($f);
    

    This will: “Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.“

    (see: http://www.php.net/manual/en/function.fopen.php)

    EDIT

    After looking a little closer at SimpleXML, you might want to have 2 files, 1 a template for the xml doc that you will load and the second will be the one you save (photo.xml). Using the fopen with the ‘w’ flag will clear the contents, but you will lose your xml structure too.

    So actually, you might not even need to use fopen at all…

    $xmlDoc=loadXMLDoc("photo_template.xml");
    
            $statistics="SELECT firstname AS 'user', count( * ) AS 'num',member_id AS ID
            FROM members
            JOIN member_photo
            USING ( member_id )
            GROUP BY firstname ";
            $result= mysql_query($statistics);
    
    
        while($row=mysql_fetch_assoc($result)) {
    
        $uploader=$xml->addChild('uploader');
        $uploader->addAttribute('ID',$row['ID']);
        $uploader->addChild('Name', $row['user']);
        $uploader->addChild('Photos_Uploaded', $row['num']);
        $xml->asXML('photo.xml');
    
        }
    

    where photo_template.xml looks like:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="user.xsl"?>
        <users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="users.xsd">
        </users>
    

    And photo.xml will end up looking like:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="user.xsl"?>
        <users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="users.xsd">
           <uploader ID="11"><Name>moz</Name><Photos_Uploaded>2</Photos_Uploaded>             </uploader>
           <uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>
           <uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>
        </users>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql database that stores some documents. A user can sign into
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have an assignment to tune a poorly performing query running on a SQL
The classic way to query an SQL database case-insensitively from Java is as follows:
We have a process updating the database which uses the following SQL IF NOT
I have sql database stored on a shared netwrok drive , after set of
We have SQL Server database setup. We are setting up a replication scenarios where
I have an SQL database with multiple tables, and I am working on creating
I have a SQL database that has a table with a field set to
I have an SQL database with a set of tables that have Unique Id's.

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.