2

Need your help as Im badly stuck here. I have MySQL database and HTML form.I can connect to my database through that form without any problem. All my buttons (FIND, DELETE, UPDATE and ADD) also work perfectly.Now I decided to add PRINT button which suppose to export retrieved data to an xml file(that will be converted to pdf file later but I know how to deal with that). Any idea how or where to start??? Is there some php function I can use?

I actually resolved that issue by creating function for my PRINT button and then calling that function later. All working perfectly.

 function printRecords($query){
 global $ID, $Name, $Address,$Country, $Ph_number;
 $result = mysql_query($query);
 $resultCount = mysql_num_rows($result);
    $query= 'SELECT ID, Name, Address , Country, Ph_number FROM people';
    $fp= @fopen ("somefile.xml","w")or
         die ("Could not open file");

    $data = "";
    for ($r=0; $r<$resultCount; $r++) {

        $rec = mysql_fetch_assoc($result);
        $data = sprintf("<Name>%s</Name>\r\n<Address>%s</Address>\r\n
        <Ph_number>%s</Ph_number>\r\n"
        $rec['Name'], $rec['Address'], $rec['Ph_number'] );

        fwrite($fp, $data);
    }
    @fclose($fp);
    return($resultCount);
 }
3
  • "printable" XML ? example ? Commented Oct 7, 2013 at 15:04
  • Ok bad wording, sorry. Just need the data retrieved from database and displayed in my html form (name, address and ph.number) to be printed in xml format. Hope its more clear now. Commented Oct 7, 2013 at 15:12
  • PHP cannot do anything about printing, other than perhaps generating a page that is suitably formatted, e.g. a PDF. printing is purely a client-side/browser thing. Commented Oct 7, 2013 at 15:14

2 Answers 2

2

Tried xml export via mysql command line ?

e.g.

mysql -uroot -p --xml -e 'SELECT * FROM mydb.Table1' > table1.xml

See the MySQL manual command options for xml

And I just saw that you have access to the database thru an html form, and maybe can't run a command line?

Otherwise you could do your own xmldoc from a select, a simple query I made here SQLFIDDLE.

create table animal(id int, name char(10));

insert into animal values
(1,'dog'),
(2,'cat'),
(3,'mouse'),
(4,'horse'),
(5,'cow');

SELECT 
  CONCAT('\n<animals>\n', 
  GROUP_CONCAT('<animal id=', id, '>',name,'</animal>\n' SEPARATOR ''), 
  '</animals>') AS xmldoc 
FROM animal;

Output would be:

<animals> 
  <animal id=1>dog</animal> 
  <animal id=2>cat</animal> 
  <animal id=3>mouse</animal> 
  <animal id=4>horse</animal> 
  <animal id=5>cow</animal> 
</animals>

The sqlfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, will have a look at that and let you know how did I get on. Thanks a mill.
Ok, that make more sense to me now :) Thanks a mill
0

You can connect to your database via php application and then with a custom functionality yu can export data like xml, cvs, excel or html whatever you need.

To export xml format via php check this link. it works

How can I export to an XML file instead of printing it on screen?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.