joeandrews91 Posted December 14, 2011 Share Posted December 14, 2011 I have the following code, and really can't see what is wrong with it. Any help would be great. <?php if (!mysql_connect('127.0.0.1', 'root', '')) { echo 'Could not connect to mysql'; exit; } $dbname = 'Requests'; $result = mysql_list_tables($dbname); if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit; } while($row2 = mysql_fetch_row($result)) { foreach ($row2[0] as $table_id) { $query = "SELECT * FROM $table_id"; $dbresult = mysql_query($query); // added code to use the tablename and select all records from that table // create a new XML document $doc = new DomDocument('1.0'); // create root node $root = $doc->createElement('mixes'); $root = $doc->appendChild($root); // process one row at a time while($row = mysql_fetch_assoc($dbresult)) { // add node for each row $occ = $doc->createElement($table_id); $occ = $root->appendChild($occ); // add a child node for each field foreach ($row as $fieldname => $fieldvalue) { $child = $doc->createElement($fieldname); $child = $occ->appendChild($child); $value = $doc->createTextNode($fieldvalue); $value = $child->appendChild($value); } // foreach } // while } } echo 'Wrote: ' . $doc->save("s.xml") . ' bytes'; // Wrote: 72 bytes ?> Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted December 14, 2011 Share Posted December 14, 2011 well..whats wrong with it? If it is not working as you intend, try finding out how far does it works as intended by placing echo statements at different parts of code. Regards, Sandeep Quote Link to comment Share on other sites More sharing options...
joeandrews91 Posted December 14, 2011 Author Share Posted December 14, 2011 Hi, Thanks for the reply, the first foreach loop doesn't work. I am unsure if you can select from all tables in the database. If i just choose one table to be printed to xml everything works fine. However, i need to print the data from every table to xml, and can't see how to do this. Will this work with a foreach loop. I am quite new to this, so apologies if I am confusing, I'm confusing myself. The code below is what i have problems with. $table_id = array(); while($rows = mysql_fetch_array($result, MYSQL_NUM)) { if (!$rows) { echo 'MySQL Error: ' . mysql_error(); exit; } } foreach ($rows[0] as $table_id){ $query = "SELECT * FROM $table_id"; $dbresult = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted December 16, 2011 Share Posted December 16, 2011 It is still not clear.. But I think there is issue here foreach ($row2[0] as $table_id) { Becauese $row2[0] is not an array but the table name. So you dont need a foreach to get its value Just the statment $table_id = $row2[0]; will do . so replace foreach ($row2[0] as $table_id) { loop with statement $table_id = $row2[0]; and try... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.